現在、私はJavaScriptベースのspotify-web-api-nodeモジュールの型を記述していますので、Typescriptプロジェクトで使用でき、コンパイラでうまく動作します。外部ノードモジュールのクラスを定義するための正しいメソッド
以下は現在tsc
型チェックを行っていますが、TSlintはInterfaces cannot be constructed. Did you mean declare class?
という文句を言います。 SpotifyInstance
をinterface
からclass
に切り替えると、tsc TS2351 Cannot use 'new' with an expression whose type lacks a call or construct signature.
によってコンパイルできなくなります。
このクラスを定義してエクスポートして、モジュールを他の場所にインポートするときに使用する正しい方法は何ですか?
spotify.d.ts
declare module "spotify-web-api-node" {
let instance: Spotify.SpotifyInstance;
export = instance;
}
declare namespace Spotify {
export interface Authorization {
body: {
access_token: string;
refresh_token: string;
};
}
export interface CurrentSong {
body: {
item: {
id: string;
name: string;
artists: [{
name: string;
}]
album: {
name: string;
images: [{
url: string
}]
}
}
};
}
export interface Config {
clientId: string;
clientSecret: string;
redirectUri: string;
}
export interface SpotifyInstance {
new(config: Config): SpotifyInstance;
createAuthorizeURL(permissions: string[], state: string): string;
authorizationCodeGrant(state: string): Promise<Authorization>;
getMyCurrentlyPlaying(): Promise<CurrentSong>;
setAccessToken(token: string): void;
setRefreshToken(token: string): void;
}
}
index.ts
import * as Spotify from "spotify-web-api-node";
const spotify = new Spotify({
clientId: "",
clientSecret: "",
redirectUri: ""
});
'最初のコードでは、私のタイピングのボタンでSpotifyInstance'は、私が現在、定義するために使用しているものです外部モジュール(spotify-web-api-node)のクラス。これは間違っていますが、クラスを定義するためにインターフェイスを使用することはできませんが、私が知りたいのは、入力ファイル内の外部モジュールからクラスを定義するための正しい構文です。 – Dustin
元のコードはjava-scriptにあり、あなたは.dファイルを構築しています。 – freedeveloper
あなたはそれを持っています!私はタイピングを書くことを試みている外部モジュールへのリンクを確実に追加して、Typescriptコンパイラでうまくいきながらモジュールを使用できるようにしました。 – Dustin