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
Below we use the following as a placeholder type. The specific "value" type in these examples is immaterial.
typeAValue={hello: 'world'};
Example 1
In the below example, t0 will be of type string because T will have type string instead of the narrower string literal type you might expect. This is the result with or without as T in the index signature but is even more surprising given the cast.
This example is important only in that it establishes how T has type string, the ramifications of this are more clear in the examples following.
typeMappedTypeWithKeyReturn={[TinstringasT]: T;}// type should be 'aKey' not string typet0=MappedTypeWithKeyReturn['aKey'];
Example 2
A slightly more complex example showing we cant use extends in the value portion as might be expected. This example fails even if we do T in string as T for the index signature, and fails even when V is not a generic but statically known.
typeMappedTypeWithConditionalValue<Vextendsstring,R>={[Tinstring]: TextendsV ? R : unknown;}// type should be { hello: 'world' } not unknowntypet1=MappedTypeWithConditionalValue<'aKey',AValue>['aKey'];
Example 3
This example shows we cannot use an extends check properly within the index signature for any V that
is not simply string.
typeMappedTypeWithConstraint<Vextendsstring,R>={[TinstringasTextendsV ? T : never]: R;}// type should be { hello: 'world' } not an errortypet2=MappedTypeWithConstraint<'aKey',AValue>['aKey'];
Example 4
A more complex example showing that the above issues prevent using a template string check
with a mapped type, via either index or value approach.
typeIndexStr= `com.some.thing.${string}`;// type should be { hello: 'world' } not unknowntypet3=MappedTypeWithConditionalValue<IndexStr,AValue>['com.some.thing.foo'];// type should be { hello: 'world' } not an errortypet4=MappedTypeWithConstraint<IndexStr,AValue>['com.some.thing.foo'];
π Actual behavior
Was not able utilize a conditional type within either the index signature or the value of a mapped type.
π Expected behavior
Should be able utilize a conditional type within either the index signature or the value of a mapped type.
The text was updated successfully, but these errors were encountered:
Closing because what I was missing is that mapped types eagerly resolve their type, so string here is correct and is also not serving as "the union of all strings". If a union could be constructed this pattern will work, unfortunately in my case a union cannot be constructed, and the solution I need is #26797
Bug Report
π Search Terms
Mapped Types, Conditionals, Template Types, Index Types, Index Signature
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
Below we use the following as a placeholder type. The specific "value" type in these examples is immaterial.
Example 1
In the below example,
t0
will be of type string becauseT
will have typestring
instead of the narrower string literal type you might expect. This is the result with or withoutas T
in the index signature but is even more surprising given the cast.This example is important only in that it establishes how
T
has typestring
, the ramifications of this are more clear in the examples following.Example 2
A slightly more complex example showing we cant use extends in the value portion as might be expected. This example fails even if we do
T in string as T
for the index signature, and fails even whenV
is not a generic but statically known.Example 3
This example shows we cannot use an extends check properly within the index signature for any
V
thatis not simply
string
.Example 4
A more complex example showing that the above issues prevent using a template string check
with a mapped type, via either index or value approach.
π Actual behavior
Was not able utilize a conditional type within either the index signature or the value of a mapped type.
π Expected behavior
Should be able utilize a conditional type within either the index signature or the value of a mapped type.
The text was updated successfully, but these errors were encountered: