15fae38b39
* 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
63 lines
1.2 KiB
Plaintext
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)
|
|
>"" : ""
|
|
>"" : ""
|
|
>"" : ""
|
|
>"" : ""
|
|
}
|