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
interfaceFoo{method(...args: ['a',number]|['b',string]): void};constmethodSyntax: Foo={method(kind,payload){if(kind==='a'){payload.toFixed();// error, Property 'toFixed' does not exist on type 'string | number'}if(kind==='b'){payload.toUpperCase();// error, Property 'toUpperCase' does not exist on type 'string | number'}}}
π Actual behavior
Checking kind inside the body of method() does not narrow payload from string | number.
π Expected behavior
Given #47190, I'd expect kind === 'a' to narrow payload to number, and kind === 'b' to narrow payload to string, which does happen when method is a function expression:
I don't know whether this is really a bug or just a follow-up feature request to add to #46658 / #47190. For context: I mentioned this in #47190, and @Andarist has written a PR for it at #48110.
The text was updated successfully, but these errors were encountered:
Bug Report
π Search Terms
control flow analysis, dependent parameters, methods, #47190, #48110
π Version & Regression Information
This behavior has been present since #47190 was merged, so TS4.6.
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
Checking
kind
inside the body ofmethod()
does not narrowpayload
fromstring | number
.π Expected behavior
Given #47190, I'd expect
kind === 'a'
to narrowpayload
tonumber
, andkind === 'b'
to narrowpayload
tostring
, which does happen whenmethod
is afunction
expression:And when
method
is an arrow function:And by #46266 when destructured from a rest argument:
I don't know whether this is really a bug or just a follow-up feature request to add to #46658 / #47190. For context: I mentioned this in #47190, and @Andarist has written a PR for it at #48110.
The text was updated successfully, but these errors were encountered: