yeun / open-color

Color scheme for UI design.
https://yeun.github.io/open-color/
MIT License
5.29k stars 242 forks source link

Correct type definition for CJS #99

Closed Rokt33r closed 5 years ago

Rokt33r commented 5 years ago

The current type definition won't work with CommonJS(cjs) import style, require('...') because we're lying to Typescript that we're exporting it as default. So typescript would expect that require('open-color') should look like {default: {white: ..., black: ....}}, not {white: ..., black: ....}. Yeah but it still working in some conditions like using esm with esModuleInterop prop. If esModuleInterop is true, typescript compiler mitigates the difference between esm and cjs styles. Otherwise, adopters will confront the below errors.

// cjs style importing
import oc = require('open-color') // Equivalent to `const oc = require('open-color')`

// `error TS2339: Property 'white' does not exist on type 'typeof 'open-color'` will be thrown while compiling.
oc.white

// `TypeError: Cannot read property 'default' of undefined` will be thrown on runtime while passing compiling
oc.default.white

FYI, I submit a similar problem. https://github.com/unifiedjs/unified/issues/56