After successfully running this code:
const f = <T extends string>(x: T) => x;
f("");
interface Dictionary<T> { [key: string]: T; }
const dict: Dictionary<number> = { a: 1 };
I anticipated the following code to work as well:
interface MyRecord<Key extends string, Value> { [_: Key]: Value };
However, the compiler is giving an error on _
:
An index signature parameter type must be 'string' or 'number'.
Changing Key extends string
to Key extends string | number
does not resolve the issue (same error).
What is causing it to fail and what would be a correct solution? (Avoiding the use of Any
and similar.)
Edit1:
type XY = 'x' | 'y';
const myXY: XY = 'x';
const myString: string = myXY;
Since this part works, I assumed that the same logic applies to indexed types (a subset of string
can be used in place of string
required by indexed types).