2016-06-10 69 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,因爲他們也使用它。我記得究竟是如何需要作品慢慢衰落說實話:) –