I'm attempting to utilize an interpolated string form to access a Record
type using a key that should match the record's key type. Unfortunately, it doesn't appear to be functioning as expected.
Here is a simple example:
type TypeOfKey = `chart-${number}`|`text-${number}`;
// type TypeOfKey = string; // It works when KeyType is this type
interface ReportComponent {
identifier: TypeOfKey;
}
interface Report {
components: Record<TypeOfKey, ReportComponent>;
}
const component : ReportComponent = {
identifier: 'text-2'
};
const report : Report = {
components: {
'text-2': component
}
};
if (report.components[component.identifier] !== undefined) { // Element implicitly has an 'any' type because expression of type 'TypeOfKey' can't be used to index type 'Record<TypeOfKey, ReportComponent>'
// do something
}
I am uncertain why this issue is occurring. As mentioned in the code, if I use string
as TypeOfKey
, it works without any issues.