Since this works:
const f = <T extends string>(x: T) => x;
f("");
interface Dictionary<T> { [key: string]: T; }
const dict: Dictionary<number> = { a: 1 };
I was expecting following code to work as well:
interface MyRecord<Key extends string, Value> { [_: Key]: Value };
but compiler reports on _
:
An index signature parameter type must be 'string' or 'number'.
Changing Key extends string
to Key extends string | number
does nothing (same error).
What is the reason why it fails and how would look a correct solution? (Preferably without using Any
and similar.)
Edit1:
type XY = 'x' | 'y';
const myXY: XY = 'x';
const myString: string = myXY;
Since this works, I was assuming same holds with indexed types (subset of string
can pose in a role of string
which is required by indexed type).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…