我希望我的对象的属性仅由number
编入索引。这是我用我的对象注释的类型:
var obj: { [y: number]: string };
这是我测试的方式:
obj = { x: '100', y: '20', z: 'abc', 0: 'def'}
obj['x'] = 'abcd'; // I expected TypeScript to throw an error because the property is indexed through a string, but it works fine
obj[0] = 'def'; // This works fine as I expected.
我在这里错过了什么根本的东西吗?