Closed
Description
Bug Report
π Search Terms
inferring, infer, generic type, generic, user-defined type guard, conditional type
π Version & Regression Information
- This is a bug in all versions of TypeScript
β― Playground Link
Playground link with relevant code
π» Code
type MainT = 'john' | 'doe'
type ValueT<T extends MainT> = T extends 'john' ? string : T extends 'doe' ? number : never
type ReturnT<T extends MainT> = T extends 'john' ? Uint32Array : T extends 'doe' ? Uint8Array : never
function isMainT<T extends MainT>(value: unknown, mainType: T): value is T {
return value === mainType
}
function test<T extends MainT>(key: T, value: ValueT<T>): ReturnT<T> {
if(isMainT(key, 'john')) {
const n = value.split('') // ValueT<T> is not inferred when T is known by using typeguard on `key`
return new Uint32Array() // ReturnT<T> is not inferred when T is known
}
}
π Actual behavior
When the type of the main generic type (MaintT
) is identified by a User-Defined Type Guard function, the other conditional types based on this main type are not identified and TS keeps it generic.
π Expected behavior
ReturnT<T>
and ValueT<T>
became inferred after T has been identified