Not compatible with TypeScript
See original GitHub issueLong story short - publishing d.ts should fix an issue, with a breaking change (probably it worth to complement the update with a codemod)
- import * as cx from 'classnames';
+ import cx from 'classnames';
The current version of classnames is not exposing default
in a ESM format, as well as d.ts
has no mention of it. As a result import * as cx from 'classnames'
, is a correct way to load this module from a TypeScript perspective.
However, if the result code would be compiled down using Babel, and Babel can compile TypeScript this would lead to the runtime exception - cx is not an object
The reason is _interopRequireWildcard
helper, which would convert classnames
to the {default: classnames}
, which is yet - not a function.
While webpack is managing “harmony imports” by it’s own, and managing in a correct (from TypeScript point of view) way - import *
== module.exports
, babel is more ESM modules compatible, and wildcard import is working differently in that case.
Proposal:
- update
classnames
to contain_esModule
property to make it ESM compatible. - ideally - expose ESM bundle
- update
d.ts
to contain only default export (breaking change), to resolve this issue.
See https://github.com/babel/babel/pull/10574#issuecomment-544107112 for details.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5
Seems
index.d.ts
is not published. please add the bellow section to yourpackage.json
or the workaround is just to use
@types/classnames
for a while.The
d.ts
file already containsexport default classNames
: https://github.com/JedWatson/classnames/blob/5d971e41f3b9c8047b61503952a413f00122b52c/index.d.ts#L15Which error does typescript report if you do
import classnames from "classnames"
?