TypeScript/tests/baselines/reference/unionWithIndexSignature.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.4 KiB
Plaintext

=== tests/cases/compiler/unionWithIndexSignature.ts ===
interface NumList {
kind: 'n';
>kind : "n"
[x: number]: number;
>x : number
}
interface StrList {
kind: 's';
>kind : "s"
[x: number]: string;
>x : number
}
export function foo<T extends NumList | StrList>(arr: T & (NumList | StrList)) {
>foo : <T extends NumList | StrList>(arr: T & (NumList | StrList)) => void
>arr : T & (NumList | StrList)
let zz = arr[1]; // Error
>zz : string | number
>arr[1] : string | number
>arr : T & (NumList | StrList)
>1 : 1
}
// Repro from #38102
export type TypedArray = Int32Array | Uint8Array;
>TypedArray : TypedArray
export function isTypedArray(a: {}): a is Int32Array | Uint8Array {
>isTypedArray : (a: {}) => a is Int32Array | Uint8Array
>a : {}
return a instanceof Int32Array || a instanceof Uint8Array;
>a instanceof Int32Array || a instanceof Uint8Array : boolean
>a instanceof Int32Array : boolean
>a : {}
>Int32Array : Int32ArrayConstructor
>a instanceof Uint8Array : boolean
>a : {}
>Uint8Array : Uint8ArrayConstructor
}
export function flatten<T extends number|TypedArray>(arr: T) {
>flatten : <T extends number | TypedArray>(arr: T) => void
>arr : T
if (isTypedArray(arr)) {
>isTypedArray(arr) : boolean
>isTypedArray : (a: {}) => a is Int32Array | Uint8Array
>arr : number | TypedArray
arr[1];
>arr[1] : number
>arr : TypedArray
>1 : 1
}
}