[英]Importing custom declaration files with TypeScript
對於名為foo
的npm包,我有以下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
,考慮到這 )。
我將消費更改為以下內容,並得出了結論:
/// <reference path="../../typings/foo.d.ts"/>
import * as foo from 'foo';
foo.endWorldHunger(true);
我真的需要reference
聲明嗎? 我期待,我沒有,因為我已經指定./typings
文件夾作為我的一個typeRoots
,但它可能是我不知怎么配置是錯誤的。 對我在做什么錯有任何想法嗎?
我正在使用TypeScript 2.2.1。
作為目錄typeRoots
應該看起來像node_modules
目錄,也就是你的申報文件foo
應該在typings/foo
文件夾並命名為index.d.ts
(或你應該package.json
在typings/foo
同types
物業指向聲明文件)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.