我在TypeScript中有以下接口:
interface IX {
a: string,
b: any,
c: AnotherType
}
我声明了一个该类型的变量并初始化了所有属性
let x: IX = {
a: 'abc',
b: null,
c: null
}
然后在稍后的init函数中为它们赋值
x.a = 'xyz'
x.b = 123
x.c = new AnotherType()
但我不喜欢在声明对象时为每个属性指定一堆默认空值,因为它们稍后将被设置为实值。我能告诉接口默认属性我不提供为空吗?是什么让我这样做:
let x: IX = {
a: 'abc'
}
而不会产生编译器错误。现在它告诉我了
TS2322:类型“{}”不能赋值给类型
“九”。属性“b”在类型“{}”中缺失。
这要看情况和用法而定。通常,在TypeScript中,接口没有默认值。
如果您不使用默认值
你可以声明x为:
let x: IX | undefined; // declaration: x = undefined
然后,在你的init函数中,你可以设置实值:
x = {
a: 'xyz'
b: 123
c: new AnotherType()
};
这样,x可以是undefined或defined - undefined表示对象未初始化,如果不需要默认值,则不设置默认值。这在逻辑上比定义“垃圾”要好。
如果你想部分赋值对象:
你可以用可选属性定义类型,比如:
interface IX {
a: string,
b?: any,
c?: AnotherType
}
在这种情况下,您只需要设置a。其他类型用?这意味着它们是可选的,并具有未定义的默认值。
甚至
let x: Partial<IX> = { ... }
这使得所有字段都是可选的。
在任何情况下,你都可以使用undefined作为默认值,这只是取决于你的用例。
我能告诉接口默认属性我不提供为空吗?是什么让我这么做的
不。不能为接口或类型别名提供默认值,因为它们仅在编译时使用,并且默认值需要运行时支持
替代
但是没有指定的值在JavaScript运行时默认为undefined。所以你可以把它们标记为可选:
interface IX {
a: string,
b?: any,
c?: AnotherType
}
现在当你创建它时,你只需要提供一个:
let x: IX = {
a: 'abc'
};
你可以根据需要提供这些值:
x.a = 'xyz'
x.b = 123
x.c = new AnotherType()
我使用下面的模式:
创建工具类型默认值<T>:
type OptionalKeys<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? K : never }[keyof T];
type Defaults<T> = Required<Pick<T, OptionalKeys<T>>>
用选项/默认值声明类:
// options passed to class constructor
export interface Options {
a: string,
b?: any,
c?: number
}
// defaults
const defaults: Defaults<Options> = {
b: null,
c: 1
};
export class MyClass {
// all options in class must have values
options: Required<Options>;
constructor(options: Options) {
// merge passed options and defaults
this.options = Object.assign({}, defaults, options);
}
}
创建类实例:
const myClass = new MyClass({
a: 'hello',
b: true,
});
console.log(myClass.options);
// { a: 'hello', b: true, c: 1 }
另一种方法是使用Pick实用程序类型并选择您希望设置为必需的属性。
interface IX {
a: string,
b: any,
c: AnotherType
}
let x: Pick<IX, 'a'> = {
a: 'abc'
}
然后当你想要声明真正的IX对象时,你只需将默认值与新值合并,如下所示:
const newX: IX = {
...x,
b: 'b',
c: () => {}
}
这个答案摘自“如何设置TypeScript接口的默认值?”