简体   繁体   English

Typescript界面​​中的可选字段

[英]Optional field in Typescript interface

On an Angular 7 project I have the following Typescript interface: 在Angular 7项目中,我具有以下Typescript接口:

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

I then use it as follows: 然后,按如下方式使用它:

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

I get an error because I am not setting limit ... 我收到错误消息,因为我未设置limit ...

How can I make limit optional in the interface? 如何在界面中将limit可选?

Typescript 2.1 introduced Partial type : Typescript 2.1引入了Partial类型

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

Another way is to make the limit optional: 另一种方法是使limit可选:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM