2016-06-10 60 views
1

我很可能在这里丢失了一些tsconfig选项。在模块中自动换行手稿定义文件

什么IM做的是非常简单的:

我创建一个新公共管理模块,例如:

export class HelloWorld { 
    constructor(public greeting: string){} 
} 

我tsconfig之中:

{ 
    "compilerOptions": { 
    "target": "ES5", 
    "module": "commonjs", 
    "removeComments": true, 
    "noImplicitAny": false, 
    "preserveConstEnums": true, 
    "declaration": true, 
    "suppressImplicitAnyIndexErrors": true, 
    "outDir": "../js" 
    }, 
    "filesGlob": [ 
    "./**/*.ts", 
    "!./node_modules/**/*.ts" 
    ] 
} 

时会自动创建我的声明它看起来像这样:

export declare class HelloWorld { 
    greeting: string; 
    constructor(greeting: string); 
} 

但是,实际上在其他项目中安装npm软件包时,这种方法效果不佳。当我导入包我必须使用:

import hello = require("hello-world/js/index"); 

(例如)

我发现,当我包裹声明文件中模块declare module "hello-world" {...}import hello = require("hello-world");

如期望然后,我可以将其导入

手动包装生成的定义文件不是一个选项,因为它会一直重写自己,是否有任何选项会从package.json中获取包的名称并自动将定义包装在该模块中?

+0

你尝试“出口默认类的HelloWorld”? – baryo

+0

@baryo是没有任何区别 – Tom

+0

可以请你发布你的package.json吗? – baryo

回答

1

请确保您有在package.json指定typings属性...

{ 
    "name": "hello-world", 
    "typings": "hello-world.d.ts", 
    ...etc... 
} 

...或更改定义文件被命名为index.d.ts

然后用这些变化在安装包后,编译器将能够解决它,当你写:

import hello = require("hello-world"); 

More details

+0

'从'hello-world'导入{HelloWorld};新的HelloWorld(“在这里问候”);'而不是要求? –

+0

@IvanZlatev我总是使用es6模块,但我只是使用require,因为他们也使用它。我记得究竟是如何需要作品慢慢衰落说实话:) –