我目前正在为基于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: ""
});
答案 0 :(得分:-1)
我在代码中没有看到类的位置。在由Typescript编译之后,接口不会在js中生成任何代码。如果你使用一个类,那么你应该为它创建一个构造函数。
export class YourClassName{
constructor() {
// Constructor Code
}
}
然后你可以这样做:
var myClass = new YourClassName();
我希望这可以帮到你