Last active
June 22, 2021 17:54
-
-
Save danieldietrich/5bf87a099884adb874d74eee6d367488 to your computer and use it in GitHub Desktop.
How to define interoperable TypeScript modules (use Rollup.js to build cjs, mjs and umd modules)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MyName { | |
export type MyType = string | number; | |
export const myValue: MyType = createSampleValue(); | |
// internal | |
function createSampleValue(): string { | |
return "test"; | |
} | |
} | |
export = MyName; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"module": "commonjs", | |
"moduleResolution": "node", | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CommonJS | |
const MyName = require('my-module'); | |
const x = MyName.myValue; | |
console.log(x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ECMAScript Module (ESM) | |
import MyName from "my-module"; | |
const x = MyName.myValue; | |
console.log(x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TypeScript | |
import MyName, { MyType } from "my-module"; | |
const x: MyType = MyName.myValue; | |
console.log(x); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment