Home > Net >  How can I extend this interface with Typescript
How can I extend this interface with Typescript

Time:10-31

I'm using the following package: https://www.npmjs.com/package/@types/spotify-api

In its index.d.ts file it has the following interface:

interface TrackObjectSimplified {
    /**
     * The artists who performed the track.
     */
    artists: ArtistObjectSimplified[];
    /**
     * A list of the countries in which the track can be played,
     * identified by their [ISO 3166-1 alpha-2 code](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2).
     */
    available_markets?: string[] | undefined;
    /**
     * The disc number (usually `1` unless the album consists of more than one disc).
     */
    disc_number: number;
    /**
     * The track length in milliseconds.
     */
    duration_ms: number;
    /**
     * Whether or not the track has explicit lyrics (`true` = yes it does; `false` = no it does not OR unknown).
     */
    explicit: boolean;
    /**
     * Known external URLs for this track.
     */
    external_urls: ExternalUrlObject;
    /**
     * A link to the Web API endpoint providing full details of the track.
     */
    href: string;
    /**
     * The [Spotify ID](https://developer.spotify.com/documentation/web-api/#spotify-uris-and-ids) for the track.
     */
    id: string;
    /**
     * Part of the response when [Track Relinking](https://developer.spotify.com/documentation/general/guides/track-relinking-guide/) is applied.
     * If `true`, the track is playable in the given market. Otherwise, `false`.
     */
    is_playable?: boolean | undefined;
    /**
     * Part of the response when [Track Relinking](https://developer.spotify.com/documentation/general/guides/track-relinking-guide/) is applied,
     * and the requested track has been replaced with different track.
     * The track in the `linked_from` object contains information about the originally requested track.
     */
    linked_from?: TrackLinkObject | undefined;
    /**
     * Part of the response when [Track Relinking](https://developer.spotify.com/documentation/general/guides/track-relinking-guide/) is applied,
     * the original track is not available in the given market, and Spotify did not have any tracks to relink it with.
     * The track response will still contain metadata for the original track, and a restrictions object containing the reason
     * why the track is not available: `"restrictions" : {"reason" : "market"}`.
     */
    restrictions?: RestrictionsObject | undefined;
    /**
     * The name of the track.
     */
    name: string;
    /**
     * A link to a 30 second preview (MP3 format) of the track. Can be null
     */
    preview_url: string | null;
    /**
     * The number of the track. If an album has several discs, the track number is the number on the specified disc.
     */
    track_number: number;
    /**
     * The object type: “track”.
     */
    type: "track";
    /**
     * The [Spotify URI](https://developer.spotify.com/documentation/web-api/#spotify-uris-and-ids) for the track.
     */
    uri: string;
}

This is not up to date. How can I configure it with more fields? I want to add for this interface the following:

interface TrackObjectSimplified {
    album: string;
    ...restOfFields...
}

How can I make it work? something related with my custom @types folder? Because I do have in my main project @types folder

CodePudding user response:

inside @types folder, create new directory spotify-api. Then, within the last created, create a file index.d.ts.

In this file:

export {};

declare global {
    declare namespace SpotifyApi {
        interface WHATEVER extends TrackObjectSimplified {
            album: string;
        }
    }
}

You can also "append" new fields without overriding existing ones:

export {};

declare global {
    declare namespace SpotifyApi {
        interface TrackObjectSimplified {
            album: string;
        }
    }
}

This would just append the album keys to the existing keys

CodePudding user response:

You can extend an interface like this:

interface MyTrackObjectSimplified extends TrackObjectSimplified {
    album: string;
}

CodePudding user response:

You just need to add the extends and create your own interface

interface MyInterface extends TrackObjectSimplified  {
    album: string;
}
  • Related