Closed
Description
Sorry for the gratuitous title.
Code
type IsString<X> = [X] extends [string] ? true : false
function isString<X extends string, Y>(x: X, y: Y) {
if (typeof x === "string") {
const a: IsString<typeof x> = true; // error: conditional type is deferred
}
if (typeof y === "string") {
const b: IsString<typeof y> = true;
}
}
Expected behavior:
The two to behave the same; probably, they should both be OK.
Actual behavior:
The type of x
does not get narrowed by assertion, presumably because the assertion adds no information. Conditional type resolution ignores parameter constraints when attempting to resolve using the most restrictive instantiation, and therefore IsString<typeof x>
does not resolve.
Essentially T extends string
, and T & string
where T
is unconstrained, do not behave the same in conditional check types. The former has its constraint ignored, while the latter gets to use its intersection proof.
Playground Link: link
Related Issues:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment