向Interface添加新属性

时间:2017-10-29 01:43:14

标签: javascript angular typescript ionic3

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

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

2 个答案:

答案 0 :(得分:1)

定义class implements interface

export class DtoLibraryItem implements LibraryItem{
    //need to declare all the properties of the interface here
    isPhotoSelected: boolean
}

答案 1 :(得分:0)

你试过declaration merging吗?这似乎更符合你所要求的,而不是你目前接受的答案:

// 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
}

希望有所帮助;祝你好运!