I am trying some TS index signature examples and feel the restriction behavior for the key is very inconsistent.
const s = Symbol();
type DictNumber = {
[key: number]: string;
}
const dictNumber: DictNumber ={
1: 'andy', // no error, which is what I defined.
'foo': 'bar', // Error, because the key is string not a number.
[s]: 'baz', // no error, but why ??? [s] is a symbol not a number right?
}
type DictString = {
[key: string]: string;
}
const dictString: DictString={
1: 'andy', // no error? why? is it converted from number to string for me?
'foo': 'bar', // no error, which is what I defined.
[s]: 'baz', // no error, but why ??? [s] is a symbol not a string right?
}
type DictSymbol= {
[key: symbol]: string;
}
const dictSymbol: DictSymbol={
1: 'andy', // Error, because the key is number not a symbol.
'foo': 'bar', // no error, why?
[s]: 'baz', // no error, which is what I defined.
I have noImplicitAny
on and alwaysStrict
on as well. here is the playground
I may be missing something really basic, can someone explain to me why this happened?