2017-07-27 5 views
1

現在、私はJavaScriptベースのspotify-web-api-nodeモジュールの型を記述していますので、Typescriptプロジェクトで使用でき、コンパイラでうまく動作します。外部ノードモジュールのクラスを定義するための正しいメソッド

以下は現在tsc型チェックを行っていますが、TSlintはInterfaces cannot be constructed. Did you mean declare class?という文句を言います。 SpotifyInstanceinterfaceから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: "" 
}); 

答えて

-1

私はクラスがどこにあるか、あなたのコード内で参照いけません。インタフェースは、Typescriptによってコンパイルされた後、jsにコードを生成しません。クラスを使用している場合は、そのコンストラクタを作成する必要があります。

export class YourClassName{ 

     constructor() { 
      // Constructor Code 
     } 
    } 

次に、あなたが行うことができます。

var myClass = new YourClassName(); 

を、私はこれはあなたに役立つことを願っ

+0

'最初のコードでは、私のタイピングのボタンでSpotifyInstance'は、私が現在、定義するために使用しているものです外部モジュール(spotify-web-api-node)のクラス。これは間違っていますが、クラスを定義するためにインターフェイスを使用することはできませんが、私が知りたいのは、入力ファイル内の外部モジュールからクラスを定義するための正しい構文です。 – Dustin

+0

元のコードはjava-scriptにあり、あなたは.dファイルを構築しています。 – freedeveloper

+0

あなたはそれを持っています!私はタイピングを書くことを試みている外部モジュールへのリンクを確実に追加して、Typescriptコンパイラでうまくいきながらモジュールを使用できるようにしました。 – Dustin

関連する問題