|
| 1 | +=== tests/cases/conformance/controlFlow/typeGuardsTypeParameters.ts === |
| 2 | + |
| 3 | +// Type guards involving type parameters produce intersection types |
| 4 | + |
| 5 | +class C { |
| 6 | +>C : Symbol(C, Decl(typeGuardsTypeParameters.ts, 0, 0)) |
| 7 | + |
| 8 | + prop: string; |
| 9 | +>prop : Symbol(C.prop, Decl(typeGuardsTypeParameters.ts, 3, 9)) |
| 10 | +} |
| 11 | + |
| 12 | +function f1<T>(x: T) { |
| 13 | +>f1 : Symbol(f1, Decl(typeGuardsTypeParameters.ts, 5, 1)) |
| 14 | +>T : Symbol(T, Decl(typeGuardsTypeParameters.ts, 7, 12)) |
| 15 | +>x : Symbol(x, Decl(typeGuardsTypeParameters.ts, 7, 15)) |
| 16 | +>T : Symbol(T, Decl(typeGuardsTypeParameters.ts, 7, 12)) |
| 17 | + |
| 18 | + if (x instanceof C) { |
| 19 | +>x : Symbol(x, Decl(typeGuardsTypeParameters.ts, 7, 15)) |
| 20 | +>C : Symbol(C, Decl(typeGuardsTypeParameters.ts, 0, 0)) |
| 21 | + |
| 22 | + let v1: T = x; |
| 23 | +>v1 : Symbol(v1, Decl(typeGuardsTypeParameters.ts, 9, 11)) |
| 24 | +>T : Symbol(T, Decl(typeGuardsTypeParameters.ts, 7, 12)) |
| 25 | +>x : Symbol(x, Decl(typeGuardsTypeParameters.ts, 7, 15)) |
| 26 | + |
| 27 | + let v2: C = x; |
| 28 | +>v2 : Symbol(v2, Decl(typeGuardsTypeParameters.ts, 10, 11)) |
| 29 | +>C : Symbol(C, Decl(typeGuardsTypeParameters.ts, 0, 0)) |
| 30 | +>x : Symbol(x, Decl(typeGuardsTypeParameters.ts, 7, 15)) |
| 31 | + |
| 32 | + x.prop; |
| 33 | +>x.prop : Symbol(C.prop, Decl(typeGuardsTypeParameters.ts, 3, 9)) |
| 34 | +>x : Symbol(x, Decl(typeGuardsTypeParameters.ts, 7, 15)) |
| 35 | +>prop : Symbol(C.prop, Decl(typeGuardsTypeParameters.ts, 3, 9)) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +function f2<T>(x: T) { |
| 40 | +>f2 : Symbol(f2, Decl(typeGuardsTypeParameters.ts, 13, 1)) |
| 41 | +>T : Symbol(T, Decl(typeGuardsTypeParameters.ts, 15, 12)) |
| 42 | +>x : Symbol(x, Decl(typeGuardsTypeParameters.ts, 15, 15)) |
| 43 | +>T : Symbol(T, Decl(typeGuardsTypeParameters.ts, 15, 12)) |
| 44 | + |
| 45 | + if (typeof x === "string") { |
| 46 | +>x : Symbol(x, Decl(typeGuardsTypeParameters.ts, 15, 15)) |
| 47 | + |
| 48 | + let v1: T = x; |
| 49 | +>v1 : Symbol(v1, Decl(typeGuardsTypeParameters.ts, 17, 11)) |
| 50 | +>T : Symbol(T, Decl(typeGuardsTypeParameters.ts, 15, 12)) |
| 51 | +>x : Symbol(x, Decl(typeGuardsTypeParameters.ts, 15, 15)) |
| 52 | + |
| 53 | + let v2: string = x; |
| 54 | +>v2 : Symbol(v2, Decl(typeGuardsTypeParameters.ts, 18, 11)) |
| 55 | +>x : Symbol(x, Decl(typeGuardsTypeParameters.ts, 15, 15)) |
| 56 | + |
| 57 | + x.length; |
| 58 | +>x.length : Symbol(String.length, Decl(lib.d.ts, --, --)) |
| 59 | +>x : Symbol(x, Decl(typeGuardsTypeParameters.ts, 15, 15)) |
| 60 | +>length : Symbol(String.length, Decl(lib.d.ts, --, --)) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// Repro from #13872 |
| 65 | + |
| 66 | +function fun<T>(item: { [P in keyof T]: T[P] }) { |
| 67 | +>fun : Symbol(fun, Decl(typeGuardsTypeParameters.ts, 21, 1)) |
| 68 | +>T : Symbol(T, Decl(typeGuardsTypeParameters.ts, 25, 13)) |
| 69 | +>item : Symbol(item, Decl(typeGuardsTypeParameters.ts, 25, 16)) |
| 70 | +>P : Symbol(P, Decl(typeGuardsTypeParameters.ts, 25, 25)) |
| 71 | +>T : Symbol(T, Decl(typeGuardsTypeParameters.ts, 25, 13)) |
| 72 | +>T : Symbol(T, Decl(typeGuardsTypeParameters.ts, 25, 13)) |
| 73 | +>P : Symbol(P, Decl(typeGuardsTypeParameters.ts, 25, 25)) |
| 74 | + |
| 75 | + const strings: string[] = []; |
| 76 | +>strings : Symbol(strings, Decl(typeGuardsTypeParameters.ts, 26, 9)) |
| 77 | + |
| 78 | + for (const key in item) { |
| 79 | +>key : Symbol(key, Decl(typeGuardsTypeParameters.ts, 27, 14)) |
| 80 | +>item : Symbol(item, Decl(typeGuardsTypeParameters.ts, 25, 16)) |
| 81 | + |
| 82 | + const value = item[key]; |
| 83 | +>value : Symbol(value, Decl(typeGuardsTypeParameters.ts, 28, 13)) |
| 84 | +>item : Symbol(item, Decl(typeGuardsTypeParameters.ts, 25, 16)) |
| 85 | +>key : Symbol(key, Decl(typeGuardsTypeParameters.ts, 27, 14)) |
| 86 | + |
| 87 | + if (typeof value === "string") { |
| 88 | +>value : Symbol(value, Decl(typeGuardsTypeParameters.ts, 28, 13)) |
| 89 | + |
| 90 | + strings.push(value); |
| 91 | +>strings.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) |
| 92 | +>strings : Symbol(strings, Decl(typeGuardsTypeParameters.ts, 26, 9)) |
| 93 | +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) |
| 94 | +>value : Symbol(value, Decl(typeGuardsTypeParameters.ts, 28, 13)) |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
0 commit comments