2016-08-24 39 views
0

我要創造我自己的分型DefinitelyTyped的排名項目(https://github.com/maxcnunes/ranking我應該怎麼做來創建自己的分型DefinitelyTyped具有默認爲

我已經嘗試以下一些實施...

declare module 'ranking' { 
    interface RankingPlayer { 
    position: number 
    score: number 
    playerId: any 
    } 

    interface Ranking { 
    maxScore: number 
    branchFactor: number 
    players: any 
    find(query); 
    findOne(query); 
    setScore(player: RankingPlayer); 
    addPlayerPoints(player: RankingPlayer); 
    } 

    interface RankingStatic { 
    (config: any): Ranking; 
    } 

    var ranking: RankingStatic; 

    export = ranking; 
} 

當我試着使用代碼

import Ranking from 'ranking' 
... 
console.log(Ranking) // This out '[Function: Ranking]' 
const ranking = new Ranking({}) // This Error 'Cannot call a class as a function' 

所以我用以下代碼再試一次......

declare module 'ranking' { 
    interface RankingPlayer { 
    position: number 
    score: number 
    playerId: any 
    } 

    export default class Ranking { 
    maxScore: number 
    branchFactor: number 
    players: any 
    constructor(config: any) 
    find(query) 
    findOne(query) 
    setScore(player: RankingPlayer) 
    addPlayerPoints(player: RankingPlayer) 
    } 
} 

,及代碼

import Ranking from 'ranking' 
... 
console.log(Ranking) // This out 'undefined' 
const ranking = new Ranking({}) // This Error 'ranking_1.default is not a constructor' 

不知道什麼應該做,以實現這個:(

+0

我認爲這不是你的打字問題,而不是你的構建過程。你能補充一點嗎? –

+0

@SebastianSebald我不知道什麼是構建過程? –

+0

由於您必須將您的TypeScript代碼轉換爲JavaScript,我想您有某種過程可以實現這一點。或者是IDE中顯示的錯誤? –

回答

1

好像Ranking是一類,所以我也把它定義爲這樣的嘗試:

定製分型/ ranking.d.ts

declare module 'ranking' { 
    class Ranking { 
    constructor(options?: {}) 
    } 
    export = Ranking 
} 

然後,你可以這樣調用它:

app.ts

import Ranking = require('ranking'); 
const ranking = new Ranking({ 
    maxScore: 1000000, 
    branchFactor: 1000 
}); 

注意使用import Ranking = require('ranking')代替import Ranking from 'ranking'。關於差異的全面討論,我可以參考this github issue

另外我回答了very similar question,你可能想看看。

+0

好的,這是工作,謝謝你;) –