You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
typeIsString<X>=[X]extends[string] ? true : falsefunctionisString<Xextendsstring,Y>(x: X,y: Y){if(typeofx==="string"){consta: IsString<typeofx>=true;// error: conditional type is deferred}if(typeofy==="string"){constb: IsString<typeofy>=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.
Sorry for the gratuitous title.
Code
Expected behavior:
The two to behave the same; probably, they should both be OK.
Actual behavior:
The type of
xdoes 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 thereforeIsString<typeof x>does not resolve.Essentially
T extends string, andT & stringwhereTis 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: = true; // error: conditional type is deferred } if (typeof y === "string") { const b: IsString = true;
}
}" rel="nofollow">link
Related Issues: