2017-10-29 16 views
0

私は以下のようにAPI interfaceを持っています。私のコントロール下にないので、私はそれに任意のプロパティを追加することはできません。isPhotoSelected: boolean = false;のようなブール値のプロパティを含める必要があります。それをどうやるか教えてくれますか?新しいプロパティをInterfaceに追加

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

答えて

1

interfaceimplementsclassを定義します。

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

はい、私は今、この種のソリューションに取り組んでいます。しかし、クラスに同じ名前を使用することはできません(つまり、 'LibraryItem')。私たちはそれを避けることができないことを望みますか? – Sampath

+0

はい、別の名前を付ける必要があります。 – Christian

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 
} 

がんばろう!

関連する問題