0

I have an API interface like below. I cannot add any property to it since it is not under my control.But I need to include boolean property like isPhotoSelected: boolean = false; to it. Can you tell me how to do that?

export interface LibraryItem {
    id: string;
    photoURL: string;
    thumbnailURL: string;
    fileName: string;
}
Sampath
  • 63,341
  • 64
  • 307
  • 441

2 Answers2

1

Define a class that implements the interface.

export class DtoLibraryItem implements LibraryItem{
    //need to declare all the properties of the interface here
    isPhotoSelected: boolean
}
Sampath
  • 63,341
  • 64
  • 307
  • 441
Christian
  • 2,676
  • 3
  • 15
  • 25
0

Have you tried declaration merging? That seems to be more in line with what you're asking for than your currently accepted answer:

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

Hope that helps; good luck!

jcalz
  • 264,269
  • 27
  • 359
  • 360