[英]Getting errors: How can I properly consume my transpiled and typed typescript npm module from typescript?
I'm still giving a shot at typescript. 我仍在尝试打字稿。 I've written a trivial "hello world" typescript module and published it to npm .
我编写了一个简单的“ hello world”打字稿模块,并将其发布到npm 。 Really trivial, just does a default export:
确实很简单,只是执行默认导出:
export default function hello(target: string = 'World'): void
{
console.log(`Hello, ${target} :-(`)
}
It is consumed perfectly well by node.js 0.10 -> 6. The module also has a proper "typings"
property in package.json, pointing to an existing .d.ts
file generated by tsc as explained in the official documentation : node.js 0.10-> 6可以很好地使用它。该模块在package.json中还具有适当的
"typings"
属性,指向tsc生成的现有.d.ts
文件, 如官方文档中所述 :
export default function hello(target?: string): void;
However, I can't get it consumed by typescript code, neither in typescript 1.8 nor 2 : 但是,无论是打字稿1.8还是2,我都无法通过打字稿代码使用它:
import hello = require('hello-world-emo')
hello()
hello('Offirmo')
Transpiling with tsc
or executing with ts-node
both give the same error message: 使用
tsc
编译或使用ts-node
执行都给出相同的错误消息:
TSError: ⨯ Unable to compile TypeScript
hello.ts (3,1): Cannot invoke an expression whose type lacks a call signature. (2349)
However, the generated .js works. 但是,生成的.js可以正常工作。 It just seems a typing issue.
似乎只是一个打字问题。 Using other import formats like
import hello from 'hello-world-emo'
doesn't work either. 使用其他导入格式(例如
import hello from 'hello-world-emo'
也不起作用。
What is typescript complaining ? 什么是打字稿抱怨? What did I miss ?
我错过了什么 ?
In case you want to inspect tsconfig
, the module is here and I'm consuming it from here 如果您想检查
tsconfig
,则模块在这里 ,我从这里使用它
The problem lies not in typescript but rather in ES6/CommonJS interop : there is no perfect matching between ES6 and CommonJS exports, especially for the ES6 "export default" , cf. 问题不在于打字稿,而在于ES6 / CommonJS互操作 :ES6和CommonJS导出之间没有完美的匹配, 尤其是ES6“默认导出” ,参见。 this article and this open issue on webpack .
本文和webpack上的此未解决问题 。
Here the module code is written in ES6 using ES6 modules. 这里,模块代码是使用ES6模块在ES6中编写的。 The build procedure is:
生成过程为:
tsc
(typescript compiler) tsc
打字稿转换为ES6(打字稿编译器) rollup
+babel for: rollup
+ babel捆绑在一起,用于:
Node 4 (stable) being the 1st class citizen, the corresponding bundle (ES6@node4 / CommonJS) is exposed via the npm "main" entry. 节点4(稳定)是一等公民,相应的捆绑包(ES6 @ node4 / CommonJS)通过npm“ main”条目公开。 It has been converted to CommonJS as best as rollup could do, making it consumable by node 4 but no longer by typescript .
它已尽最大可能将其转换为CommonJS,使其可以在节点4上使用,而不再可以通过typescript来使用 。
Another option would have been to transpile to ES5/CommonJs with tsc
, which adds special annotations for linking back CommonJS to ES6 modules . 另一个选择是使用
tsc
转换为ES5 / CommonJs, 它添加了特殊注释,用于将CommonJS链接回ES6模块 。 But then it's 1) no longer bundled 2) more roughly transpiled (not using ES6 features included in node 4) and 3) more hardly consumable by node 但是,这是1)不再捆绑2)更粗糙地转译(不使用节点4中包含的ES6功能)和3)节点几乎不消耗
A compromise solution : avoid the CommonJS / ES6 modules interop problem by not using export default
but only named exports. 一个折衷的解决方案 :通过不使用
export default
而是仅使用命名的export default
避免CommonJS / ES6模块互操作问题。 This mitigation technique only downside is for node 4 : 此缓解技术的唯一缺点是节点4:
import hello from '...'
becomes import { hello } from '...'
OK import hello from '...'
import { hello } from '...'
成为import hello from '...'
import { hello } from '...'
好 const hello = require('...')
becomes const { hello } = require('...')
OK const hello = require('...')
成为const { hello } = require('...')
OK var hello = require('...')
becomes var hello = require('...').hello
less elegant but acceptable var hello = require('...')
变为var hello = require('...').hello
不太优雅但可以接受
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.