如何在TypeScript中声明一个嵌套对象数组的对象?
我有两个像这样的类。如何在TypeScript中声明一个嵌套对象数组的对象?
class Stuff {
constructor() { }
things: Thing[] = [];
name: string;
}
class Thing {
constructor() { }
active: boolean;
}
我试图在我的应用程序中声明这样一个字段。
blopp: Stuff[] = [
{name: "aa", things: null},
{name: "bb", things: null}];
上述方法工作得很好。但是,当我尝试提供一组数组而不是null时,我得到的错误是它不能指定指定的类型。
blopp: Stuff[] = [
{name: "aa", things: [{active: true}, {active: false}]},
{name: "bb", things: null}];
你应该使用new
关键字来实例化对象:
class Stuff {
constructor(public name: string, public things: Thing[] = []) { }
}
class Thing {
constructor(public active: boolean) {
};
}
var blopp: Stuff[] = [
new Stuff("aa", [new Thing(true), new Thing(false)]),
new Stuff("bb", null)
];
或者干脆使用接口:
interface IThing {
active: boolean
}
interface IStuff {
name: string;
things: IThing[]
}
var blopp: IStuff[] = [
{ name: "aa", things: [{ active: true }, { active: false }] },
{ name: "bb", things: null }];
重要的是要确定是否需要类或接口是非常重要的有些东西不适用于匿名对象:
/*
class Stuff {
\t constructor(public name: string, public things: Thing[] = []) { }
}
class Thing {
\t constructor(public active: boolean) {
\t };
}
var blopp: Stuff[] = [
\t { name: "aa", things: [{ active: true }, { active: false }] },
\t new Stuff("bb", null)
];
console.log("Is blopp[0] Stuff:", blopp[0] instanceof Stuff);
console.log("Is blopp[1] Stuff:", blopp[1] instanceof Stuff);
*/
var Stuff = (function() {
function Stuff(name, things) {
if (things === void 0) { things = []; }
this.name = name;
this.things = things;
}
return Stuff;
}());
var Thing = (function() {
function Thing(active) {
this.active = active;
}
;
return Thing;
}());
var blopp = [
{ name: "aa", things: [{ active: true }, { active: false }] },
new Stuff("bb", null)
];
console.log("Is blopp[0] Stuff:", blopp[0] instanceof Stuff);
console.log("Is blopp[1] Stuff:", blopp[1] instanceof Stuff);
在JavaScript中使用* interface *感觉超级怪异。我仍然习惯于* class *的概念。 (我已经在C#中做了超过15年的OO,但是在JS中它似乎充满异国情调......)你会说类在一般情况下更合适还是被广泛使用?我无法判断... –
Typescript对类最有效,因为这是它的预期用途。如果您需要将一些JavaScript耦合到您的手稿,接口很不错。 –
尝试使用<>
或as
关键字铸造:
blopp: Stuff[] = [
{name: "aa", things: [{active: true} as Thing , {active: false}as Thing]},
{name: "bb", things: null}];
}
或
blopp: Stuff[] = [
{name: "aa", things: [<Thing>{active: true} , <Thing>{active: false}]},
{name: "bb", things: null}];
}
您会如何评价您的建议与其他答复中Emil提出的基于构造函数的建议?我需要更多的反馈来做出明智的决定。 :) –
其实看起来不错,对我说:https://www.typescriptlang.org/播放/ index.html的#SRC =类%20Stuff%20%7B%0D 0A%%20%20constructor()%20%7B%20%7D%0D 0A%%20%20things%3A%20Thing%5B%5D% 20%3D%20%5B%5D%3B%0D 0A%%20%20name%3A%20string%3B%0D 0A%%7D%0D 0A%%0D %0Aclass%20Thing%20%7B%0D%0A%20%20constructor()%20%7B%20%7D%0D%0A%20%20active%3A%20boolean%3B%0D%0A%7D%0D%0A %0D%0Alet%20blopp%3A%20Stuff%5B%5D%20%3D%20%5B%0D%0A%20%20%7Bname%3A%20%22个氨基酸%22%2C%20things%3A%20%5B %7Bactive%3A%20true%7D%2C%20%7Bactive%3A%20false%7D%5D%7D%2C%20%0D%0A%20%20%7Bname%3A%20%22BB%22%2C%20things %3A%20null%7D%5D%3B%0D%0A – deceze
@deceze不知道该告诉你什么。我收到了一条错误消息。然而,从接受的答案提出的建议发挥了诀窍。也许我的IDE更加烦人(或者你的不够耐用)。不够精通TS,无法通过判断。 –