-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Conditions for conditional types of function parameters don't propagate into function bodies #50371
Comments
Resolving of conditional types is deferred when they involve unbound type argumente. This is a design limitation. |
Is there a way to explicitly carry the resolution that occurs in |
There's not really a great way to describe the sorts of behavior where functions want to talk about "keys See #48992 |
assuming this is a simpler example of the same issue type Foo<T extends string> = T extends string ? T : never
const makeFoo = <T extends string>(value: T): Foo<T> => value // error |
Got a similar error function foo<T extends true | false>(arg: T): T extends true ? string : number {
return arg === true ? 'yes' : 0 // Type 'number' cannot be assigned to type 'T extends true? String: number'. ts(2322)
} How to implement the function of Conditional Types? No example is found in the document. |
@mengdu i believe that's a different issue. it's caused by the fact that narrowing a value with a generic type doesn't necessarily mean it's safe to narrow the generic itself. this can be demonstrated by adding a second argument to your function: type Foo<T extends string> = T extends string ? T : never
const makeFoo = <T extends string>(value: T): Foo<T> => value // error
function foo<T extends true | false>(arg: T, arg2: T): T extends true ? string : number {
if (arg === true) {
// these two lines have errors, because narrowing arg can't narrow T because it would be unsafe to do so
const foo: true = arg2 //arg2 can be false
return 'yes'
} else {
return 0
}
}
foo<boolean>(true, false) see #21732 (comment) for another example |
@RyanCavanaugh The initial test case still fails type check on 5.5.0-dev.20240324. Why was this closed as completed? |
Bug Report
Even when TypeScript proves that conditional type conditions hold, these proofs aren't fully propagated into the function body.
🔎 Search Terms
conditional type propagation witness entails function parameter
🕗 Version & Regression Information
⏯ Playground Link
Playground Link
💻 Code
🙁 Actual behavior
TypeScript fails to type check this code, with the following error:
🙂 Expected behavior
TypeScript successfully type checks this code. It can already prove that
MyMap[K] extends T
in order to bindkey
with typeK
.The text was updated successfully, but these errors were encountered: