6aeb8c12cc
* Create separate types for equivalent aliased unions * Accept new baselines * Preserve original types for union types * Accept new baselines * Preserve intersection origin for union types * Accept new baselines * Accept new baselines * Preserve aliases during relationship checks * Accept new baselines * Preserve aliases for intersection and indexed access types * Accept new baselines * Compute intersection-of-unions cross product without recursion * Accept new baselines * Use denormalized type objects for origin / support 'keyof' origins * Accept new baselines * Fix fourslash test * Recursively extract named union types * Accept new baselines * Map on union origin in mapType to better preserve aliases and origins * Remove redundant call * Accept new baselines * Revert back to declared type when branches produce equivalent union * Accept new baselines * Don't include denormal origin types in regular type statistics * Fix issue with unions not being marked primitive-only * Allow new alias to be associated with type alias instantiation * Accept new baselines * Revert "Accept new baselines" This reverts commit4507270cc1
. * Revert "Allow new alias to be associated with type alias instantiation" This reverts commit2c2d06dfe1
.
74 lines
1.9 KiB
Plaintext
74 lines
1.9 KiB
Plaintext
=== tests/cases/compiler/assertionFunctionsCanNarrowByDiscriminant.ts ===
|
|
interface Cat {
|
|
type: 'cat';
|
|
>type : "cat"
|
|
|
|
canMeow: true;
|
|
>canMeow : true
|
|
>true : true
|
|
}
|
|
|
|
interface Dog {
|
|
type: 'dog';
|
|
>type : "dog"
|
|
|
|
canBark: true;
|
|
>canBark : true
|
|
>true : true
|
|
}
|
|
|
|
type Animal = Cat | Dog;
|
|
>Animal : Animal
|
|
|
|
declare function assertEqual<T>(value: any, type: T): asserts value is T;
|
|
>assertEqual : <T>(value: any, type: T) => asserts value is T
|
|
>value : any
|
|
>type : T
|
|
|
|
const animal = { type: 'cat', canMeow: true } as Animal;
|
|
>animal : Animal
|
|
>{ type: 'cat', canMeow: true } as Animal : Animal
|
|
>{ type: 'cat', canMeow: true } : { type: "cat"; canMeow: true; }
|
|
>type : "cat"
|
|
>'cat' : "cat"
|
|
>canMeow : true
|
|
>true : true
|
|
|
|
assertEqual(animal.type, 'cat' as const);
|
|
>assertEqual(animal.type, 'cat' as const) : void
|
|
>assertEqual : <T>(value: any, type: T) => asserts value is T
|
|
>animal.type : "cat" | "dog"
|
|
>animal : Animal
|
|
>type : "cat" | "dog"
|
|
>'cat' as const : "cat"
|
|
>'cat' : "cat"
|
|
|
|
animal.canMeow; // since is cat, should not be an error
|
|
>animal.canMeow : true
|
|
>animal : Cat
|
|
>canMeow : true
|
|
|
|
const animalOrUndef = { type: 'cat', canMeow: true } as Animal | undefined;
|
|
>animalOrUndef : Animal | undefined
|
|
>{ type: 'cat', canMeow: true } as Animal | undefined : Animal | undefined
|
|
>{ type: 'cat', canMeow: true } : { type: "cat"; canMeow: true; }
|
|
>type : "cat"
|
|
>'cat' : "cat"
|
|
>canMeow : true
|
|
>true : true
|
|
|
|
assertEqual(animalOrUndef?.type, 'cat' as const);
|
|
>assertEqual(animalOrUndef?.type, 'cat' as const) : void
|
|
>assertEqual : <T>(value: any, type: T) => asserts value is T
|
|
>animalOrUndef?.type : "cat" | "dog" | undefined
|
|
>animalOrUndef : Animal | undefined
|
|
>type : "cat" | "dog" | undefined
|
|
>'cat' as const : "cat"
|
|
>'cat' : "cat"
|
|
|
|
animalOrUndef.canMeow; // since is cat, should not be an error
|
|
>animalOrUndef.canMeow : true
|
|
>animalOrUndef : Cat
|
|
>canMeow : true
|
|
|