Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将在构造函数中重写的Typescript接口默认值_Javascript_Typescript - Fatal编程技术网

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> = {  }
公共只读设置:部分={}