Typescript 界面中的可选字段

Mig*_*ura 2 typescript angular angular6 angular7

在 Angular 7 项目中,我有以下 Typescript 界面:

export interface Request {
  expand: string;
  limit: number;
} 
Run Code Online (Sandbox Code Playgroud)

然后我按如下方式使用它:

let request: Request = { expand: 'address' };
Run Code Online (Sandbox Code Playgroud)

我收到错误,因为我没有设置limit...

如何limit在界面中设置可选?

yur*_*zui 6

Typescript 2.1 引入了Partial 类型

let request: Partial<Request> = { expand: 'address' };
Run Code Online (Sandbox Code Playgroud)

另一种方法是使limit可选:

export interface Request {
  expand: string;
  limit?: number;
} 
Run Code Online (Sandbox Code Playgroud)