Node.js 在模块中自动换行Typescript定义文件
我很可能错过了一些tsconfig选项 我所做的很简单: 我正在创建一个npm模块,例如:Node.js 在模块中自动换行Typescript定义文件,node.js,typescript,npm,Node.js,Typescript,Npm,我很可能错过了一些tsconfig选项 我所做的很简单: 我正在创建一个npm模块,例如: export class HelloWorld { constructor(public greeting: string){} } 我的tsconfig是: { "compilerOptions": { "target": "ES5", "module": "commonjs", "removeComments": true, "noImplicitAny"
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");
(例如)
我发现,当我将声明文件包装在模块中声明模块“hello world”{…}
中时,我可以根据需要通过import hello=require(“hello world”)导入它代码>
手动包装生成的定义文件不是一个选项,因为它会一直重写自身。是否有任何选项可以从package.json
中获取包名并自动包装该模块中的定义?请确保在package.json
中指定了typings
属性
{
"name": "hello-world",
"typings": "hello-world.d.ts",
...etc...
}
…或将定义文件更改为名为index.d.ts
然后,在安装带有这些更改的包之后,编译器将能够在您编写以下代码时解析它:
import hello = require("hello-world");
您是否尝试过“导出默认类HelloWorld”?@baryo yes没有任何区别。请发布您的package.json?看看这篇文章,它解释了typescript在哪里查找键入,等等:从“HelloWorld”导入{HelloWorld};新HelloWorld(“在此问候”)代码>而不是require?@IvanZlatev我总是使用es6模块,但我只是使用require,因为他们也使用它。老实说,我对require工作原理的记忆正在慢慢消失:)