Open
Description
Bug Report
π Search Terms
template string key generic "is not assignable to type"
π Version & Regression Information
- This changed between versions 4.6.4 and 4.7.4
β― Playground Link
Playground link with relevant code
π» Code
// ObjectWithUnderscoredKeys creates an object where:
// Keys are the strings in K, except each with an underscore prefix
// Values are all `true`
export type ObjectWithUnderscoredKeys<K extends string> = {
[k in K as `_${k}`]: true;
};
// it works without generics
function nonGenericTest(objectWithUnderscoredKeys: ObjectWithUnderscoredKeys<"foo" | "bar">, key: "foo" | "bar") {
const shouldBeTrue: true = objectWithUnderscoredKeys[`_${key}`];
}
// but not with generics
function genericTest<K extends string>(objectWithUnderscoredKeys: ObjectWithUnderscoredKeys<K>, key: K) {
// ERROR:
// Type 'ObjectWithUnderscoredKeys<K>[`_${K}`]' is not assignable to type 'true'.
// Type 'ObjectWithUnderscoredKeys<K>[`_${string}`]' is not assignable to type 'true'.
const shouldBeTrue: true = objectWithUnderscoredKeys[`_${key}`];
}
π Actual behavior
In the generic function genericTest
, indexing into the object doesn't yield a value with the type true
.
π Expected behavior
In the generic function genericTest
, indexing into the object should yield a value with the type true
.
Related issues
This seems similar to #48983, but that was marked as Working as intended
and the behavior in this example doesn't seem like it should be intended.