我想做一個具有動態屬性的介面,例如這個物件:
我想做一個具有動態屬性的介面,例如這個物件:
test: testInterface [];
this.test = [
{
name: {
text: 'something',
anotherThing: 'something'.
}
},
{
foo: {
text: 'something'。
anotherThing: 'something'.
}
},
{
whatever: {
text: 'something',
anotherThing: 'something'.
}
},
];
text,foo和什么(可以是任何名字,陣列可以有任何數量的專案)
我試過這樣的方法:
export interface testInterface {
[propName: string]: testSubInterface;
}
介面 testSubInterface {
text: string;
anotherThing: string;
但是這對我來說并不奏效,最終的想法是像這樣改變資料:
this.testInterface。 foo.text = "另一個文本為例"。
任何想法?
uj5u.com熱心網友回復:
你的介面是正確的,但你的物件宣告是錯誤的。
你的test應該被宣告為一個物件,而不是一個陣列。
const test: testInterface = {
name: {
text: "something"。
anotherThing: "something"。
},
foo: {
text: "東西"。
anotherThing: "something"。
},
whatever: {
text: "東西"。
anotherThing: "something".
}
};
this.testInterface.foo.text = "另一個文本為例"。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/307929.html
標籤:
上一篇:通用物件中一個鍵的型別保護函式
