Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by desmap on Thursday August 06, 2020 @06:04AM
(#24069580)
But how do you solve it in a tsc context?
Using the default prop doesn't work in tsc and breaks all type information because tsc just doesn't know of any default prop.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by fireattack on Thursday August 06, 2020 @08:53PM
(#24076929)
Not exactly sure what you mean but you can check this: https://github.com/microsoft/TypeScript/issues/2719
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by desmap on Thursday August 06, 2020 @11:48PM
(#24077996)
Thanks for the link. I meant following case:
A TS file which compiles to an es6 ("esnext") module cannot import cjs. Either node breaks or TS' type-checking fails. Former if you omit default when importing and latter if you introduce .default in the TS codebase.
It's a bit complicated, what I just wrote about is reflected in the chart in step 4-6 here https://github.com/microsoft/TypeScript/issues/18442#issueco...
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by fireattack on Thursday August 06, 2020 @05:56AM
(#24069547)
Typo: I meant to say "... using `export default xxx`...".
When converting TypeScript that using `import default xxx` to JS using `tsc`, it would convert it to something like `exports.default = xxx`.
To import it, you have to use `const XXX = require('./mo.js').default` instead of just `const XXX = require('./mo.js')` in plain ES6 JS.
This is fine but Babel used to [1] be able to convert `const XXX = require('./mo.js')` to valid code, which made lots of people wrote it (incorrectly) that way (I was very baffled why people wrote like that since it does not work in plain ES6).
[1] https://github.com/babel/babel/issues/2212