![](/img/trans.png)
[英]how do i add new property to existing interface and then export the new interface in Typescript?
[英]Add a new property to Interface
我有一个如下所示的API interface
。 我无法在其中添加任何属性,因为它不受我的控制。但是我需要包括boolean属性,例如isPhotoSelected: boolean = false;
对此。 你能告诉我怎么做吗?
export interface LibraryItem {
id: string;
photoURL: string;
thumbnailURL: string;
fileName: string;
}
定义一个implements
interface
的class
。
export class DtoLibraryItem implements LibraryItem{
//need to declare all the properties of the interface here
isPhotoSelected: boolean
}
您是否尝试过声明合并 ? 这似乎比您目前所接受的答案更符合您的要求:
// import from module
import { LibraryItem } from 'librarymodule';
// locally augment the module's interface declaration
declare module './playground' {
export interface LibraryItem {
isPhotoSelected: boolean
}
}
// use it
const libtaryItem: LibraryItem = {
id: 'id',
photoURL: 'https://example.com/photo.jpg',
fileName: 'fileName.ext',
thumbnailURL: 'https://example.com/thumbnail.jpg',
isPhotoSelected: true
}
希望能有所帮助; 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.