-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
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:
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed