-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conditional type doesn't narrow primitive types #26240
Comments
IMHO, this's issue is slightly related to #26199. It looks like TS doesn't properly handle underlying JavaScript types. I mean, each TS type should have two components:
All type computations should be based on these two independent components. |
Wouldn't this be perfectly fine if you just did |
Looks like not limited to primitive types. |
What's wrong with this? type ToNumber2<T extends number | string> =
//OK
T extends number ? OnlyNumber<T> : undefined |
@AnyhowStep yes, it's workaround for the bug which works fine for the specific sample case. |
This is just a manifestation of how we don't track negated constraints in the |
I think I've run into the same issue but in a different context--just going to add this here to put a snippet in for anyone searching for answers for the same type of problem. type OptionalFn = (() => string) | undefined
// Type 'OptionalFn' does not satisfy the constraint '(...args: any[]) => any'.
// Type 'undefined' is not assignable to type '(...args: any[]) => any'
type OptionalFnReturnType = OptionalFn extends undefined ? undefined : ReturnType<OptionalFn> Can workaround by manually narrowing type: type OptionalFn = (() => string) | undefined
type OptionalFnReturnType = OptionalFn extends undefined ? undefined : ReturnType<NonNullable<OptionalFn>> (See |
TypeScript Version: 3.1.0-dev.201xxxxx
Search Terms:
Code
Expected behavior:
No errors
Actual behavior:
Error:
Playground Link: https://www.typescriptlang.org/play/#src=type%20OnlyNumber%3CT%20extends%20number%3E%20%3D%20T%0D%0A%0D%0Atype%20ToNumber%3CT%20extends%20number%20%7C%20string%3E%20%3D%0D%0A%20%20%20%20T%20extends%20string%20%3F%20undefined%20%3A%20OnlyNumber%3CT%3E%0D%0A
Related Issues:
The text was updated successfully, but these errors were encountered: