TypeScript/tests/baselines/reference/functionCallOnConstrainedTypeVariable.types
Anders Hejlsberg 15fae38b39
Improve narrowing of generic types in control flow analysis (#43183)
* Narrow type variables with union constraints when merited by contextual type

* Narrow generics with union type constraints as indicated by contextual type

* Accept new baselines

* Add tests

* Fix circularity for JSX elements

* Remove unnecessary isConstraintPosition information from flow cache key

* Update comment

* Add additional tests

* Rename to getNarrowableTypeForReference, remove getConstraintForLocation

* Add comment

* Fix removal of undefined in destructurings with initializers

* Use getContextFreeTypeOfExpression in discriminateContextualTypeByObjectMembers

* In obj[x], use constraint of obj's type only when x's type is non-generic

* Add comment
2021-03-19 17:12:57 -07:00

63 lines
1.2 KiB
Plaintext

=== tests/cases/compiler/functionCallOnConstrainedTypeVariable.ts ===
// Repro from #20196
type A = {
>A : A
a: (x: number) => string
>a : (x: number) => string
>x : number
};
type B = {
>B : B
a: (x: boolean) => string
>a : (x: boolean) => string
>x : boolean
};
function call0(p: A | B) {
>call0 : (p: A | B) => void
>p : A | B
p.a("s"); // Error
>p.a("s") : string
>p.a : ((x: number) => string) | ((x: boolean) => string)
>p : A | B
>a : ((x: number) => string) | ((x: boolean) => string)
>"s" : "s"
}
function callN<T extends A | B>(p: T) {
>callN : <T extends A | B>(p: T) => void
>p : T
p.a("s"); // Error
>p.a("s") : string
>p.a : ((x: number) => string) | ((x: boolean) => string)
>p : A | B
>a : ((x: number) => string) | ((x: boolean) => string)
>"s" : "s"
var a: T["a"] = p.a;
>a : T["a"]
>p.a : ((x: number) => string) | ((x: boolean) => string)
>p : A | B
>a : ((x: number) => string) | ((x: boolean) => string)
a(""); // Error
>a("") : string
>a : ((x: number) => string) | ((x: boolean) => string)
>"" : ""
a("", "", "", ""); // Error
>a("", "", "", "") : string
>a : ((x: number) => string) | ((x: boolean) => string)
>"" : ""
>"" : ""
>"" : ""
>"" : ""
}