Javascript 将在构造函数中重写的Typescript接口默认值
我有一个界面,它有一个属性Javascript 将在构造函数中重写的Typescript接口默认值,javascript,typescript,Javascript,Typescript,我有一个界面,它有一个属性matchMaker,看起来像这样: export interface ServerOptions { matchMaker: MatchMakerType<MatchMaker> } export class Server { public readonly settings: ServerOptions = { matchMaker: undefined } public constructor(options: Server
matchMaker
,看起来像这样:
export interface ServerOptions {
matchMaker: MatchMakerType<MatchMaker>
}
export class Server {
public readonly settings: ServerOptions = {
matchMaker: undefined
}
public constructor(options: ServerOptions) {
this.settings = Object.assign(this.settings, options)
}
}
创建类Server
的新实例时,我希望在将选项传递给构造函数时,为MatchMaker
设置typeof MatchMaker
。我不希望将undefined | null
或任何其他内容传递给构造函数,但是我需要将一些值作为默认值,当我使用将被覆盖的undefined
时,我会得到一个错误:
类型“undefined”不可分配给类型“MatchMakerType”
如何设置将被覆盖的默认设置?您可以使用
Partial
,以允许服务器中未定义的属性。设置
:
public readonly settings: Partial<ServerOptions> = { }
公共只读设置:部分={}