對於名爲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