File: assignmentGenericLookupTypeNarrowing.types

package info (click to toggle)
node-typescript 3.3.3333-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 324,548 kB
  • sloc: makefile: 6; sh: 3
file content (43 lines) | stat: -rw-r--r-- 1,099 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
=== tests/cases/conformance/expressions/assignmentOperator/assignmentGenericLookupTypeNarrowing.ts ===
// Repro from #26130

let mappedObject: {[K in "foo"]: null | {x: string}} = {foo: {x: "hello"}};
>mappedObject : { foo: { x: string; }; }
>null : null
>x : string
>{foo: {x: "hello"}} : { foo: { x: string; }; }
>foo : { x: string; }
>{x: "hello"} : { x: string; }
>x : string
>"hello" : "hello"

declare function foo<T>(x: T): null | T;
>foo : <T>(x: T) => T
>x : T
>null : null

function bar<K extends "foo">(key: K) {
>bar : <K extends "foo">(key: K) => void
>key : K

  const element = foo(mappedObject[key]);
>element : { foo: { x: string; }; }[K]
>foo(mappedObject[key]) : { foo: { x: string; }; }[K]
>foo : <T>(x: T) => T
>mappedObject[key] : { foo: { x: string; }; }[K]
>mappedObject : { foo: { x: string; }; }
>key : K

  if (element == null)
>element == null : boolean
>element : { foo: { x: string; }; }[K]
>null : null

    return;
  const x = element.x;
>x : string
>element.x : string
>element : { foo: { x: string; }; }[K]
>x : string
}