2017-03-01 854 views
0

對於名爲foo的npm軟件包,我有以下TypeScript聲明(爲了本示例的目的,我們只假設它)沒有任何可以提取的聲明文件。使用TypeScript導入自定義聲明文件

declare module "foo" { 
    export function getPerpetualEnergy(): any[]; 
    export function endWorldHunger(n: boolean): void; 
} 

我已經把該聲明中./typings/foo.d.ts文件,並更新了我的typeRoots文件夾下面(內./tsconfig.json文件):

{ 
    "compilerOptions": { 
     "outDir": "./dist/", 
     "sourceMap": true, 
     "noImplicitAny": true, 
     "module": "commonjs", 
     "target": "es5", 
     "jsx": "react", 
     "strictNullChecks": true, 
     "moduleResolution": "node", 
     "typeRoots": [ 
      "./typings/", 
      "./node_modules/@types/" 
     ] 
    }, 
    "include": [ 
     "src/**/*" 
    ], 
    "exclude": [ 
     "node_modules", 
     "**/*.test.ts" 
    ] 
} 

,我曾嘗試使用它從一個.ts象下面這樣:

import * as foo from 'foo'; 
foo.endWorldHunger(true); 

但是,它無法解決這個問題。我在VS Code裏面抱怨我(因爲我有noImplicitAny: true,我想,考慮this)。

我在下面改變了我的消費來和它的工作了:

/// <reference path="../../typings/foo.d.ts"/> 
import * as foo from 'foo'; 
foo.endWorldHunger(true); 

我真的需要一個reference聲明?我期待我沒有,因爲我已經指定./typings文件夾作爲我的typeRoots之一,但有可能我以某種方式配置了錯誤。有什麼想法我做錯了什麼?

我正在使用TypeScript 2.2.1。作爲typeRoots

回答

0

目錄應該看起來像node_modules目錄,即對於foo您的聲明文件應在typings/foo文件夾,命名爲index.d.ts(或你應該有package.jsontypings/footypes屬性指向聲明文件)。