自动将模板打字稿定义文件自动包装在模块中
我很可能在这里丢失了一些tsconfig选项。
我在做什么很简单:
我正在创build一个npm模块,例如:
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" ] }
当我的声明是自动创build它看起来像这样:
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
中获取包的名称,并自动将定义包装在该模块中?
确保你有在package.json
指定的typings
属性…
{ "name": "hello-world", "typings": "hello-world.d.ts", ...etc... }
…或者将你的定义文件index.d.ts
为index.d.ts
。
然后,在安装包含这些更改的包后,编译器将能够在编写时解决它:
import hello = require("hello-world");
更多细节