-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Computed generic argument type is not narrowed #44358
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
Comments
There's a workaround to use tuple type for arguments type SelectArgs =
| ['a', string]
| ['b', number]
const select = (...args: SelectArgs) => {
if (args[0] === 'b') return args[1] * 5;
return 1;
} But in that case one cannot destructure them, because then it would break for some reason, so have to keep using indexes :( |
Because such an operation would be unsound:
You would need #27808 first. You have the very common misconception that |
So is the only way right now to do something like this is to use tuples like in #44358 (comment) (or analogous object type) without the ability to destructure? |
Thanks for links, looks like it depends on existing issues. |
Bug Report
π Search Terms
computed generic function argument guard narrow
π Version & Regression Information
v4.3.2
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
Cannot
data * 5
becauseThe left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.(2362)
π Expected behavior
data * 5
is valid because it's within the code branch wheretype === 'b'
and type ofdata
in that case isnumber
The text was updated successfully, but these errors were encountered: