Skip to content

Instantly share code, notes, and snippets.

@danieldietrich
Last active June 22, 2021 17:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danieldietrich/5bf87a099884adb874d74eee6d367488 to your computer and use it in GitHub Desktop.
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)
namespace MyName {
export type MyType = string | number;
export const myValue: MyType = createSampleValue();
// internal
function createSampleValue(): string {
return "test";
}
}
export = MyName;
{
"module": "commonjs",
"moduleResolution": "node",
}
// CommonJS
const MyName = require('my-module');
const x = MyName.myValue;
console.log(x);
// ECMAScript Module (ESM)
import MyName from "my-module";
const x = MyName.myValue;
console.log(x);
// 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