typescript 打字稿索引签名示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typescript 打字稿索引签名示例相关的知识,希望对你有一定的参考价值。

class Animal {
    name: string = "";
}
class Dog extends Animal {
    breed: string = "";
}

interface NumberDictionary {
    [index: string]: number|string;
    length: number;    // ok, length is a number
    name: string;      // error, the type of 'name' is not a subtype of the indexer
}

let test:NumberDictionary = {length:2, name: 'anthonywang'}

console.log(test[name])
console.log(test[0])

interface NumberDictionary1 {
    [index: string]: number;
    length: number;    // ok, length is a number
}

interface ReadonlyStringArray {
    [index: number]: string;
}
let myArray: ReadonlyStringArray = ["Alice", "Bob"];
myArray[2] = "Mallory"; // error!

console.log(myArray[0])
console.log(myArray[1])
console.log(myArray[2])

以上是关于typescript 打字稿索引签名示例的主要内容,如果未能解决你的问题,请参考以下文章