2017-03-01 55 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属性指向声明文件)。