在打字稿接口可选字段

问题描述 投票:0回答:1

在一个角7项目中,我有以下打字稿接口:

export interface Request {
  expand: string;
  limit: number;
} 

然后,我用它,如下所示:

let request: Request = { expand: 'address' };

我得到一个错误,因为我没有设置limit ...

我怎样才能让limit可选的界面?

angular typescript angular6 angular7
1个回答
3
投票

打字稿2.1引入了Partial type

let request: Partial<Request> = { expand: 'address' };

另一种方法是使limit可选:

export interface Request {
  expand: string;
  limit?: number;
} 
© www.soinside.com 2019 - 2024. All rights reserved.