我使用以下包:https://www.npmjs.com/package/@types spotify-api
在它的index.d.ts
文件中有以下接口:
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;
}
这不是最新的。我如何配置更多的字段?我想为这个接口添加以下内容:
interface TrackObjectSimplified {
album: string;
...restOfFields...
}
我怎样才能使它工作?与我的自定义@types
文件夹相关的东西?因为在我的主项目@types
文件夹
在@types
文件夹中创建新目录spotify-api
。然后,在最后创建的文件中,创建一个文件index.d.ts
.
在这个文件中:
export {};
declare global {
declare namespace SpotifyApi {
interface WHATEVER extends TrackObjectSimplified {
album: string;
}
}
}
你也可以"追加"。不覆盖现有字段的新字段:
export {};
declare global {
declare namespace SpotifyApi {
interface TrackObjectSimplified {
album: string;
}
}
}
这将把album
键附加到现有的键
你可以这样扩展一个接口:
interface MyTrackObjectSimplified extends TrackObjectSimplified {
album: string;
}
您只需要添加扩展并创建您自己的接口
interface MyInterface extends TrackObjectSimplified {
album: string;
}