我是TypeScript的新手,并希望为JavaScript库moviedb-js创建一个d.ts定义文件,该文件可以从TheMovieDb中检索电影信息。我在Definitely Typed repository上查找了现有的定义,并在创建定义文件时阅读了Fenton's guide,但是,我仍然坚持翻译以下JavaScript位:
var MovieDB = require('moviedb')('your api key');
创建模块时,我有类似
的内容declare module 'moviedb' { ... }
在我的TypeScript代码中,我将其导入:
import moviedb = require('moviedb')
显然,如果没有API密钥,这不起作用,并且在JavaScript示例中添加密钥也不起作用。那么如何以一种我可以定义所需API密钥的方式重写模块定义呢?
答案 0 :(得分:1)
@basarat:我已经检查了你的回复,但效果还不错。再次感谢!
作为旁注,我发现this post在理解用于添加API密钥的构造方面也非常有用。
那我该怎么办:
MovieScanner.ts
export class TmbdMovieScanner {
movieDb: MovieDB.IMovieDB;
constructor() {
this.movieDb = moviedb('YOUR API KEY');
// For testing purposes
this.movieDb.searchMovie({ query: "Aliens" }, (err, movies) => {
if (err) {
console.log('Error: ' + err);
return;
}
console.log(movies);
});
this.movieDb.movieInfo({ id: 666 }, (err, curMovie) => {
if (err) {
console.log('Error: ' + err);
return;
}
console.log(curMovie.overview);
});
}
}
moviedb.d.ts(仍然不完整):
declare module 'moviedb' {
function apiKeyAcceptor(key: string): MovieDB.IMovieDB;
export = apiKeyAcceptor;
}
declare module MovieDB {
export interface IMovieDB {
searchMovie(params: SearchOptions, callback: (err: any, movies: SearchResults) => void): void;
movieInfo(options: InfoOptions, callback: (err: any, curMovie: Movie) => void): void;
}
export interface SearchOptions {
query: string;
}
export interface InfoOptions {
id: number;
}
export interface SearchResults {
page : number;
results : SearchResult[];
total_Pages : number;
total_results: number;
}
export interface SearchResult {
adult : boolean;
backdrop_path : string;
id : number;
original_title: string;
release_date : Date;
poster_path : string;
popularity : number;
title : string;
vote_average : number;
vote_count : number;
}
export interface Movie {
adult : boolean;
backdrop_path : string;
belongs_to_collection: any;
budget : number;
genres : Genre[];
homepage : string;
id : number;
imdb_id : number;
original_title : string;
overview : string;
popularity : number;
poster_path : string;
production_companies : ProductionCompany[];
production_countries : ProductionCountry[];
release_date : Date;
revenue : number;
runtime : number;
spoken_languages : SpokenLanguage[];
status : string;
tagline : string;
title : string;
vote_average : number;
vote_count : number;
}
export interface Genre {
id : number;
name: string;
}
export interface ProductionCompany {
id : number;
name: string;
}
export interface ProductionCountry {
iso_3166_1: number;
name : string;
}
export interface SpokenLanguage {
iso_639_1: number;
name : string;
}
}
答案 1 :(得分:0)
这是概念moviedb.d.ts
declare module MovieDB{
export interface IMovieDB{
searchMovie(params:any,callback:Function):IMovieDB;
// Add other methods here
}
}
declare module 'moviedb' {
function apiKetAcceptor(key:string):MovieDB.IMovieDB;
export = apiKeyAcceptor;
}
要使用它,请参考+ import:
/// <reference path='../path/to/moviedb.d.ts'/>
import moviedb = require('moviedb');
var MovieDB = moviedb('your api key');
MovieDB.searchMovie({query: 'Alien' }, function(err, res){
console.log(res);
});
有关详细示例,请查看Node.js定义https://github.com/borisyankov/DefinitelyTyped/blob/master/node/node.d.ts