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
Suppose I have two disjoint types Foo and Bar. I make a function that takes a key that's either foo or bar. This function returns a function that takes a function called funcArg. When key is foo, funcArg takes a Foo, and when key is bar, it takes a Bar.
In my inner function body, I call funcArg. I check what key is, and pass an appropriate value. Doing so gives me this:
Argument of type '{ val: string; } | { val: number; }' is not assignable to parameter of type 'T extends "foo" ? Foo : Bar'.
Type '{ val: string; }' is not assignable to type 'T extends "foo" ? Foo : Bar'.(2345)
I have to add as T extends "foo" ? Foo : Bar to the argument when I call funcArg.
π Expected behavior
Since T is a union type, I expect TypeScript to follow the branches. When T is "foo", then funcArg will take type Foo. The key === "foo" conditional will produce a Foo. TypeScript is treating key as foo or bar all the way down into the inner function definition, when really it's already decided by that point, since key has to be one or the other.
It's like a quantum-mechanics/SchrΓΆdinger's-cat situation where key can be either "foo" or "bar" until it's "observed" at runtime. But were that so, then how does the conditional type on funcArg work? How do conditional types work at all? Also, TypeScript seems to do just fine with conditionals/ternaries at runtime, having no issues with things like typeof val === "function" ? val() : val.
The text was updated successfully, but these errors were encountered:
Bug Report
π Search Terms
conditional, generic, inference, assertion, union
π Version & Regression Information
I'm using 4.2.3. I haven't tried it in other versions, but I have no reason to believe this ever worked.
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
Suppose I have two disjoint types
Foo
andBar
. I make a function that takes akey
that's eitherfoo
orbar
. This function returns a function that takes a function calledfuncArg
. Whenkey
isfoo
,funcArg
takes aFoo
, and whenkey
isbar
, it takes aBar
.In my inner function body, I call
funcArg
. I check whatkey
is, and pass an appropriate value. Doing so gives me this:I have to add
as T extends "foo" ? Foo : Bar
to the argument when I callfuncArg
.π Expected behavior
Since
T
is a union type, I expect TypeScript to follow the branches. WhenT
is"foo"
, thenfuncArg
will take typeFoo
. Thekey === "foo"
conditional will produce aFoo
. TypeScript is treatingkey
asfoo
orbar
all the way down into the inner function definition, when really it's already decided by that point, sincekey
has to be one or the other.It's like a quantum-mechanics/SchrΓΆdinger's-cat situation where
key
can be either"foo"
or"bar"
until it's "observed" at runtime. But were that so, then how does the conditional type onfuncArg
work? How do conditional types work at all? Also, TypeScript seems to do just fine with conditionals/ternaries at runtime, having no issues with things liketypeof val === "function" ? val() : val
.The text was updated successfully, but these errors were encountered: