2017-07-28 840 views
7

我試圖提供包分型不具備他們:我應該在哪裏放置自定義的.d.ts文件?

error TS7016: Could not find a declaration file for module 'inputmask-core'. './node_modules/inputmask-core/lib/index.js' implicitly has an 'any' type. 
Try `npm install @types/inputmask-core` if it exists or add a new declaration (.d.ts) file containing `declare module 'inputmask-core';` 

我使用TS-裝載機的WebPack與打字稿2.4.2,和我有以下類型的根在成立tsconfig.json:

"typeRoots": [ 
    "./node_modules/@types", 
    "./src/client/types" 
] 

我試圖模仿封裝結構在node_modules/@types

src/client/types 
|--inputmask-core 
    |--index.d.ts 

隨着以下:

declare class InputMask {} 
export default InputMask; 

但是錯誤仍然存​​在。我究竟做錯了什麼?我應該在哪裏放置這些自定義.d.ts文件?

node_modules/@types和任何其他類型的根有什麼區別?爲什麼TypeScript對待它們的方式不同?

回答

3

可能的解決辦法:放在index.d.ts以下,它將編譯:

declare module "inputmask-core" { 
    declare class InputMask {} 
    export default InputMask; 
} 

我還是不明白的特殊處理node_modules/@types得到,雖然。

+0

'node_modules/@ types'用於保存包含沒有內置聲明的包的類型信息的第三方包。它會得到特殊的處理,因爲您不直接從它導入,而是每個聲明包都在它被看作覆蓋在它所包含類型的包上。 ''typeRoots「'可以讓你自定義位置或者添加額外的目錄,以這種特殊的方式進行處理。 –

+0

@AluanHaddad - 如果「typeRoots」中的其他目錄也以這種特殊方式得到處理,那麼爲什麼將某個包的目錄從'node_modules/@ types'移動到其他typeRoot時會破壞編譯? – Rogach

+0

這裏沒有足夠的信息來說明。沒有tsconfig,模塊解析等等。你最終得到的是一個_ambient外部module_,它有不同的分辨率語義。特別是,它不能通過文件的路徑來解決。 –

相關問題