|
| 1 | +=== tests/cases/compiler/assertionFunctionsCanNarrowByDiscriminant.ts === |
| 2 | +interface Cat { |
| 3 | +>Cat : Symbol(Cat, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 0, 0)) |
| 4 | + |
| 5 | + type: 'cat'; |
| 6 | +>type : Symbol(Cat.type, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 0, 15)) |
| 7 | + |
| 8 | + canMeow: true; |
| 9 | +>canMeow : Symbol(Cat.canMeow, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 1, 16)) |
| 10 | +} |
| 11 | + |
| 12 | +interface Dog { |
| 13 | +>Dog : Symbol(Dog, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 3, 1)) |
| 14 | + |
| 15 | + type: 'dog'; |
| 16 | +>type : Symbol(Dog.type, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 5, 15)) |
| 17 | + |
| 18 | + canBark: true; |
| 19 | +>canBark : Symbol(Dog.canBark, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 6, 16)) |
| 20 | +} |
| 21 | + |
| 22 | +type Animal = Cat | Dog; |
| 23 | +>Animal : Symbol(Animal, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 8, 1)) |
| 24 | +>Cat : Symbol(Cat, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 0, 0)) |
| 25 | +>Dog : Symbol(Dog, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 3, 1)) |
| 26 | + |
| 27 | +declare function assertEqual<T>(value: any, type: T): asserts value is T; |
| 28 | +>assertEqual : Symbol(assertEqual, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 10, 24)) |
| 29 | +>T : Symbol(T, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 12, 29)) |
| 30 | +>value : Symbol(value, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 12, 32)) |
| 31 | +>type : Symbol(type, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 12, 43)) |
| 32 | +>T : Symbol(T, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 12, 29)) |
| 33 | +>value : Symbol(value, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 12, 32)) |
| 34 | +>T : Symbol(T, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 12, 29)) |
| 35 | + |
| 36 | +const animal = { type: 'cat', canMeow: true } as Animal; |
| 37 | +>animal : Symbol(animal, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 14, 5)) |
| 38 | +>type : Symbol(type, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 14, 16)) |
| 39 | +>canMeow : Symbol(canMeow, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 14, 29)) |
| 40 | +>Animal : Symbol(Animal, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 8, 1)) |
| 41 | + |
| 42 | +assertEqual(animal.type, 'cat' as const); |
| 43 | +>assertEqual : Symbol(assertEqual, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 10, 24)) |
| 44 | +>animal.type : Symbol(type, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 0, 15), Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 5, 15)) |
| 45 | +>animal : Symbol(animal, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 14, 5)) |
| 46 | +>type : Symbol(type, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 0, 15), Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 5, 15)) |
| 47 | + |
| 48 | +animal.canMeow; // since is cat, should not be an error |
| 49 | +>animal.canMeow : Symbol(Cat.canMeow, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 1, 16)) |
| 50 | +>animal : Symbol(animal, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 14, 5)) |
| 51 | +>canMeow : Symbol(Cat.canMeow, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 1, 16)) |
| 52 | + |
| 53 | +const animalOrUndef = { type: 'cat', canMeow: true } as Animal | undefined; |
| 54 | +>animalOrUndef : Symbol(animalOrUndef, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 19, 5)) |
| 55 | +>type : Symbol(type, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 19, 23)) |
| 56 | +>canMeow : Symbol(canMeow, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 19, 36)) |
| 57 | +>Animal : Symbol(Animal, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 8, 1)) |
| 58 | + |
| 59 | +assertEqual(animalOrUndef?.type, 'cat' as const); |
| 60 | +>assertEqual : Symbol(assertEqual, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 10, 24)) |
| 61 | +>animalOrUndef?.type : Symbol(type, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 0, 15), Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 5, 15)) |
| 62 | +>animalOrUndef : Symbol(animalOrUndef, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 19, 5)) |
| 63 | +>type : Symbol(type, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 0, 15), Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 5, 15)) |
| 64 | + |
| 65 | +animalOrUndef.canMeow; // since is cat, should not be an error |
| 66 | +>animalOrUndef.canMeow : Symbol(Cat.canMeow, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 1, 16)) |
| 67 | +>animalOrUndef : Symbol(animalOrUndef, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 19, 5)) |
| 68 | +>canMeow : Symbol(Cat.canMeow, Decl(assertionFunctionsCanNarrowByDiscriminant.ts, 1, 16)) |
| 69 | + |
0 commit comments