-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 narrowing for function arguments doesn't work #29947
Comments
A function foo(error: Error | null, value: Value | undefined): void { … } One adjustment that will correctly flag the calls of function foo<T extends Error | null>(error: Error | T, value: IfNull<T, Value>) { … } To narrow If you really want narrowing in the function you might have to do something like this: function foo(...args: [Error, null] | [undefined, Value]) {
if (args[0]) {
return;
}
args[1].do();
} |
@jack-williams Thank you for clarification.
Interesting... Thanks! But I'm really more interested in narrowing inside of the function.
Great! However when I'm trying to make it more user-friendly i.e.: function foo(...[error, value]: [Error, null] | [undefined, Value]) {
if (error) {
return;
}
value.do();
} It doesn't work. Am I missing something? |
Type narrowing does not work across variables. This is a design limitation that is tracked here #12184. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
TypeScript Version: 3.4.0-dev.201xxxxx
Search Terms:
conditional type narrowing function arguments
Code
strictNullChecks
is true.Expected behavior:
Typescript should narrow types for function arguments as it does for variable declaration.
Actual behavior:
Function arguments are treated without narrowing.
Playground Link:
https://www.typescriptlang.org/play/#src=type%20IfNull%3CT%2C%20U%3E%20%3D%20T%20extends%20null%20%3F%20U%20%3A%20undefined%3B%0D%0A%0D%0Ainterface%20Value%20%7B%0D%0A%20%20do%3A%20()%20%3D%3E%20void%3B%0D%0A%7D%0D%0A%0D%0A%2F%2F%20foo%20type%20shows%20as%20%22function%20foo(error%3A%20Error%20%7C%20null%2C%20value%3A%20Value%20%7C%20undefined)%3A%20void%22%0D%0Afunction%20foo(error%3A%20Error%20%7C%20null%2C%20value%3A%20IfNull%3Ctypeof%20error%2C%20Value%3E)%20%7B%0D%0A%20%20if%20(error)%20%7B%0D%0A%20%20%20%20return%3B%0D%0A%20%20%7D%0D%0A%20%20%0D%0A%20%20%2F%2F%20error%20TS2532%3A%20Object%20is%20possibly%20'undefined'.%0D%0A%20%20value.do()%3B%0D%0A%7D%0D%0A%0D%0A%2F%2F%20Shouldn't%20be%20allowed%3A%0D%0Afoo(null%2C%20undefined)%3B%0D%0Afoo(new%20Error()%2C%20%7B%20do%3A%20()%20%3D%3E%20%7B%7D%20%7D)%3B%0D%0A%0D%0A%2F%2F%20However%20these%20work%20as%20expected%3A%0D%0Alet%20error%3A%20Error%20%7C%20null%20%3D%20null%2C%0D%0A%20%20value%3A%20IfNull%3Ctypeof%20error%2C%20Value%3E%20%3D%20%7B%20do%3A%20()%20%3D%3E%20%7B%7D%20%7D%3B%0D%0A%0D%0Avalue.do()%3B
Related Issues:
Didn't find.
The text was updated successfully, but these errors were encountered: