|
| 1 | +//// [tests/cases/compiler/inferContextualTypePredicates1.ts] //// |
| 2 | + |
| 3 | +=== inferContextualTypePredicates1.ts === |
| 4 | +type Foo = { type: "foo"; foo: number }; |
| 5 | +>Foo : Symbol(Foo, Decl(inferContextualTypePredicates1.ts, 0, 0)) |
| 6 | +>type : Symbol(type, Decl(inferContextualTypePredicates1.ts, 0, 12)) |
| 7 | +>foo : Symbol(foo, Decl(inferContextualTypePredicates1.ts, 0, 25)) |
| 8 | + |
| 9 | +type Bar = { type: "bar"; bar: string }; |
| 10 | +>Bar : Symbol(Bar, Decl(inferContextualTypePredicates1.ts, 0, 40)) |
| 11 | +>type : Symbol(type, Decl(inferContextualTypePredicates1.ts, 1, 12)) |
| 12 | +>bar : Symbol(bar, Decl(inferContextualTypePredicates1.ts, 1, 25)) |
| 13 | + |
| 14 | +declare function skipIf<A, B extends A>( |
| 15 | +>skipIf : Symbol(skipIf, Decl(inferContextualTypePredicates1.ts, 1, 40)) |
| 16 | +>A : Symbol(A, Decl(inferContextualTypePredicates1.ts, 3, 24)) |
| 17 | +>B : Symbol(B, Decl(inferContextualTypePredicates1.ts, 3, 26)) |
| 18 | +>A : Symbol(A, Decl(inferContextualTypePredicates1.ts, 3, 24)) |
| 19 | + |
| 20 | + as: A[], |
| 21 | +>as : Symbol(as, Decl(inferContextualTypePredicates1.ts, 3, 40)) |
| 22 | +>A : Symbol(A, Decl(inferContextualTypePredicates1.ts, 3, 24)) |
| 23 | + |
| 24 | + predicate: (a: A) => a is B, |
| 25 | +>predicate : Symbol(predicate, Decl(inferContextualTypePredicates1.ts, 4, 10)) |
| 26 | +>a : Symbol(a, Decl(inferContextualTypePredicates1.ts, 5, 14)) |
| 27 | +>A : Symbol(A, Decl(inferContextualTypePredicates1.ts, 3, 24)) |
| 28 | +>a : Symbol(a, Decl(inferContextualTypePredicates1.ts, 5, 14)) |
| 29 | +>B : Symbol(B, Decl(inferContextualTypePredicates1.ts, 3, 26)) |
| 30 | + |
| 31 | +): Exclude<A, B>[]; |
| 32 | +>Exclude : Symbol(Exclude, Decl(lib.es5.d.ts, --, --)) |
| 33 | +>A : Symbol(A, Decl(inferContextualTypePredicates1.ts, 3, 24)) |
| 34 | +>B : Symbol(B, Decl(inferContextualTypePredicates1.ts, 3, 26)) |
| 35 | + |
| 36 | +declare const items: (Foo | Bar)[]; |
| 37 | +>items : Symbol(items, Decl(inferContextualTypePredicates1.ts, 8, 13)) |
| 38 | +>Foo : Symbol(Foo, Decl(inferContextualTypePredicates1.ts, 0, 0)) |
| 39 | +>Bar : Symbol(Bar, Decl(inferContextualTypePredicates1.ts, 0, 40)) |
| 40 | + |
| 41 | +const r1 = skipIf(items, (item) => item.type === "foo"); // ok |
| 42 | +>r1 : Symbol(r1, Decl(inferContextualTypePredicates1.ts, 10, 5)) |
| 43 | +>skipIf : Symbol(skipIf, Decl(inferContextualTypePredicates1.ts, 1, 40)) |
| 44 | +>items : Symbol(items, Decl(inferContextualTypePredicates1.ts, 8, 13)) |
| 45 | +>item : Symbol(item, Decl(inferContextualTypePredicates1.ts, 10, 26)) |
| 46 | +>item.type : Symbol(type, Decl(inferContextualTypePredicates1.ts, 0, 12), Decl(inferContextualTypePredicates1.ts, 1, 12)) |
| 47 | +>item : Symbol(item, Decl(inferContextualTypePredicates1.ts, 10, 26)) |
| 48 | +>type : Symbol(type, Decl(inferContextualTypePredicates1.ts, 0, 12), Decl(inferContextualTypePredicates1.ts, 1, 12)) |
| 49 | + |
| 50 | +const r2 = skipIf(items, (item) => item.type === "foo" || item.type === "bar"); // ok |
| 51 | +>r2 : Symbol(r2, Decl(inferContextualTypePredicates1.ts, 11, 5)) |
| 52 | +>skipIf : Symbol(skipIf, Decl(inferContextualTypePredicates1.ts, 1, 40)) |
| 53 | +>items : Symbol(items, Decl(inferContextualTypePredicates1.ts, 8, 13)) |
| 54 | +>item : Symbol(item, Decl(inferContextualTypePredicates1.ts, 11, 26)) |
| 55 | +>item.type : Symbol(type, Decl(inferContextualTypePredicates1.ts, 0, 12), Decl(inferContextualTypePredicates1.ts, 1, 12)) |
| 56 | +>item : Symbol(item, Decl(inferContextualTypePredicates1.ts, 11, 26)) |
| 57 | +>type : Symbol(type, Decl(inferContextualTypePredicates1.ts, 0, 12), Decl(inferContextualTypePredicates1.ts, 1, 12)) |
| 58 | +>item.type : Symbol(type, Decl(inferContextualTypePredicates1.ts, 1, 12)) |
| 59 | +>item : Symbol(item, Decl(inferContextualTypePredicates1.ts, 11, 26)) |
| 60 | +>type : Symbol(type, Decl(inferContextualTypePredicates1.ts, 1, 12)) |
| 61 | + |
| 62 | +const r3 = skipIf(items, (item) => false); // error |
| 63 | +>r3 : Symbol(r3, Decl(inferContextualTypePredicates1.ts, 12, 5)) |
| 64 | +>skipIf : Symbol(skipIf, Decl(inferContextualTypePredicates1.ts, 1, 40)) |
| 65 | +>items : Symbol(items, Decl(inferContextualTypePredicates1.ts, 8, 13)) |
| 66 | +>item : Symbol(item, Decl(inferContextualTypePredicates1.ts, 12, 26)) |
| 67 | + |
| 68 | +const r4 = skipIf(items, (item) => true); // error |
| 69 | +>r4 : Symbol(r4, Decl(inferContextualTypePredicates1.ts, 13, 5)) |
| 70 | +>skipIf : Symbol(skipIf, Decl(inferContextualTypePredicates1.ts, 1, 40)) |
| 71 | +>items : Symbol(items, Decl(inferContextualTypePredicates1.ts, 8, 13)) |
| 72 | +>item : Symbol(item, Decl(inferContextualTypePredicates1.ts, 13, 26)) |
| 73 | + |
| 74 | +const pred1: (a: string | null, b: string | null) => b is string = (a, b) => typeof b === 'string'; // ok |
| 75 | +>pred1 : Symbol(pred1, Decl(inferContextualTypePredicates1.ts, 15, 5)) |
| 76 | +>a : Symbol(a, Decl(inferContextualTypePredicates1.ts, 15, 14)) |
| 77 | +>b : Symbol(b, Decl(inferContextualTypePredicates1.ts, 15, 31)) |
| 78 | +>b : Symbol(b, Decl(inferContextualTypePredicates1.ts, 15, 31)) |
| 79 | +>a : Symbol(a, Decl(inferContextualTypePredicates1.ts, 15, 68)) |
| 80 | +>b : Symbol(b, Decl(inferContextualTypePredicates1.ts, 15, 70)) |
| 81 | +>b : Symbol(b, Decl(inferContextualTypePredicates1.ts, 15, 70)) |
| 82 | + |
| 83 | +const pred2: (a: string | null, b: string | null) => b is string = (a, b) => typeof a === 'string'; // error |
| 84 | +>pred2 : Symbol(pred2, Decl(inferContextualTypePredicates1.ts, 16, 5)) |
| 85 | +>a : Symbol(a, Decl(inferContextualTypePredicates1.ts, 16, 14)) |
| 86 | +>b : Symbol(b, Decl(inferContextualTypePredicates1.ts, 16, 31)) |
| 87 | +>b : Symbol(b, Decl(inferContextualTypePredicates1.ts, 16, 31)) |
| 88 | +>a : Symbol(a, Decl(inferContextualTypePredicates1.ts, 16, 68)) |
| 89 | +>b : Symbol(b, Decl(inferContextualTypePredicates1.ts, 16, 70)) |
| 90 | +>a : Symbol(a, Decl(inferContextualTypePredicates1.ts, 16, 68)) |
| 91 | + |
0 commit comments