有没有比这更漂亮的方式来用 typeScript 中的类型成员来表达匿名类(?)?
class Foo {
member = {
aNumber = <number>undefined;
aBoolean = <bool>undefined;
}
}
有没有比这更漂亮的方式来用 typeScript 中的类型成员来表达匿名类(?)?
class Foo {
member = {
aNumber = <number>undefined;
aBoolean = <bool>undefined;
}
}
唯一的匿名选择是:
class Foo {
member: { aNumber?: number; aBoolean?: bool; } = {
aNumber: undefined,
aBoolean: undefined
}
}
你通常最好只写一个interface
这样你就可以命名类型。
Since members are undefined by default you could simply go with:
class Foo {
member:{aNumber:number;aBoolean:bool;} = <any>{};
}
在较新版本的打字稿中,这可以使用可选属性来实现:
class Foo {
member: { aNumber?: number; aBoolean?: boolean; } = {}
}