繁体   English   中英

向接口添加新属性

[英]Add a new property to Interface

我有一个如下所示的API interface 我无法在其中添加任何属性,因为它不受我的控制。但是我需要包括boolean属性,例如isPhotoSelected: boolean = false; 对此。 你能告诉我怎么做吗?

export interface LibraryItem {
    id: string;
    photoURL: string;
    thumbnailURL: string;
    fileName: string;
}

定义一个implements interfaceclass

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.

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