Typescript 界面中的可选字段
Optional field in Typescript interface
在 Angular 7 项目中,我有以下 Typescript 接口:
export interface Request {
expand: string;
limit: number;
}
然后我按如下方式使用它:
let request: Request = { expand: 'address' };
我收到一个错误,因为我没有设置 limit
...
如何在界面中使 limit
可选?
引入了 Typescript 2.1 Partial type:
let request: Partial<Request> = { expand: 'address' };
另一种方法是使 limit
可选:
export interface Request {
expand: string;
limit?: number;
}
在 Angular 7 项目中,我有以下 Typescript 接口:
export interface Request {
expand: string;
limit: number;
}
然后我按如下方式使用它:
let request: Request = { expand: 'address' };
我收到一个错误,因为我没有设置 limit
...
如何在界面中使 limit
可选?
引入了 Typescript 2.1 Partial type:
let request: Partial<Request> = { expand: 'address' };
另一种方法是使 limit
可选:
export interface Request {
expand: string;
limit?: number;
}