javascript - Getting errors: How can I properly consume my transpiled and typed typescript npm module from typescript? -


i'm still giving shot @ typescript. i've written trivial "hello world" typescript module , published npm. trivial, default export:

export default function hello(target: string = 'world'): void {     console.log(`hello, ${target} :-(`) } 

it consumed node.js 0.10 -> 6. module has proper "typings" property in package.json, pointing existing .d.ts file generated tsc as explained in official documentation :

export default function hello(target?: string): void; 

however, can't consumed typescript code, neither in typescript 1.8 nor 2 :

import hello = require('hello-world-emo')  hello() hello('offirmo') 

transpiling tsc or executing ts-node both give same error message:

tserror: ⨯ unable compile typescript hello.ts (3,1): cannot invoke expression type lacks call signature. (2349) 

however, generated .js works. seems typing issue. using other import formats import hello 'hello-world-emo' doesn't work either.

what typescript complaining ? did miss ?

in case want inspect tsconfig, module here , i'm consuming here

the problem lies not in typescript rather in es6/commonjs interop : there no perfect matching between es6 , commonjs exports, especially es6 "export default", cf. this article , this open issue on webpack.

here module code written in es6 using es6 modules. build procedure is:

  1. transpile typescript es6 tsc (typescript compiler)
  2. transpile es6 code , bundle rollup+babel for:
    • es6@node4 / commonjs (1st class citizen)
    • es5@node0.10 / commonjs (for still used legacy)
    • es5 / umd (for browsers)

node 4 (stable) being 1st class citizen, corresponding bundle (es6@node4 / commonjs) exposed via npm "main" entry. has been converted commonjs best rollup do, making consumable node 4 no longer typescript.

another option have been transpile es5/commonjs tsc, which adds special annotations linking commonjs es6 modules. it's 1) no longer bundled 2) more transpiled (not using es6 features included in node 4) , 3) more hardly consumable node

a compromise solution: avoid commonjs / es6 modules interop problem not using export default named exports. mitigation technique downside node 4 :

  • es6: import hello '...' becomes import { hello } '...' ok
  • node >=6: const hello = require('...') becomes const { hello } = require('...') ok
  • node <=4: var hello = require('...') becomes var hello = require('...').hello less elegant acceptable

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -