typescript索引类型,可索引类型-TypeScript

Below syntax,

interface StringArray {

[index: number]: string;

}

states that when a StringArray is indexed with a number, it will return a string. For example - let myArray: StringArray;myArray = ["Bob", "Fred"];let myStr: string = myArray[0];

So, myArray is type constrained on values stored as string type, by declaring it with type StringArray. Key(index) is always string type, under the hood(JavaScript), despite mentioning it as number

From the below syntax,

class Animal{

name: string;

}

class Dog extends Animal{

breed: string;

}

interface NotOkay {

[x: number]: Animal;

[x: string]: Dog;

}

1) What does NotOkay syntax state?

From the below syntax,

interface NumberDictionary {

[index: string]: number;

length: number; // ok, length is a number

name: string; // error, the type of 'name' is not a subtype of the indexer

}

2) What does NumberDictionary state?

3) Why these two syntax are wrong? error TS2413 & error: TS2411

解决方案

(as @betadeveloper points out, the examples are taken from the typescript documentation at https://www.typescriptlang.org/docs/handbook/interfaces.html)

1+3) The point the documentation is making is that this declaration is not consistent.

The declaration would seem to say that this code works:

let no: NotOkay

function byString(key: string): Dog {

return no[key]

}

function byNumber(key: number): Animal {

return no[key]

}

But the problem is that a lookup of e.g. no[0] is really the same things as no['0'] (it's how JavaScript works). So byString('0') will actually just result in an Animal, not necessarily a Dog.

So to prevent the code above – that looks correct but is in fact wrong for certain string keys (those that happen to be numbers in string form) – TypeScript does not allow the declaration.

2+3) [index: string]: number; says that you can use any string as an index of a NumberDictionary and you will get a number.

On the other hand, name: string; says that the property name is a string. But a property can also be accessed as an index, so e.g. when doing

let nd: NumberDictionary

let a = nd['name']

let b = nd.name

a and b mean exactly the same thing, but the first declaration seemed to say a was a number, whereas the second declaration seems to say b is a string. That's a contradiction and therefore this declaration is also not allowed.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值