create-react-context icon indicating copy to clipboard operation
create-react-context copied to clipboard

fix TypeScript typings exports

Open roymiloh opened this issue 6 years ago • 6 comments

Hi,

So this one is pretty interesting - currently it seems like you expect people to import create-react-context this way (because declaration file specifies export default ...):

import createReactContext from 'create-react-context';

export const context = createReactContext(null);

The TypeScript compilation result is:

// my-app/context.js
var create_react_context_1 = require("create-react-context");
exports.context = create_react_context_1.default(null);

However, in create-react-context production (transpiled) version, we have:

// create-react-context/lib/index.js
exports.default = _react2.default.createContext || _implementation2.default;
// this is the problematic line
module.exports = exports['default'];

So obviously previous create_react_context_1.default is undefined.

Two possible solutions (1) dropping the module.exports line (happens due to this plugin), or (2) accept this PR, and then client would import this way:

import * as createReactContext from 'create-react-context';
// or:
import createReactContext = require('create-react-context');

Dropping module.exports means that clients who don't use es2015 modules would've to import this way:

const {default: createReactContext} = require('create-react-context');

... and maybe it's a breaking change (although it's not documented).

I suggest to accept this PR (and keep module.export - PR is based on having it), as typings are broken anyway - no idea how people use it in current state.

@jamiebuilds please.

roymiloh avatar Jun 10 '18 22:06 roymiloh

Importing function as a namespace is the biggest failure of typescript. Key-value object just cannot be a function.

import * as createReactContext from 'create-react-context';

Some guys said me the problem can be solved with more flexible interop option.

TrySound avatar Jun 10 '18 22:06 TrySound

Failure or not (yeah, it's a workaround that TypeScript upholds) - currently it just does not work:)

roymiloh avatar Jun 10 '18 22:06 roymiloh

Workaround in every package is broken ecosystem.

TrySound avatar Jun 14 '18 17:06 TrySound

Here to confirm the problem. The library can not be consumed by a Typescript project as of now.

rhalukongor avatar Jun 15 '18 14:06 rhalukongor

Can't you play with some options to solve the problem on your side?

TrySound avatar Jun 15 '18 15:06 TrySound

Example to support all options : https://github.com/DefinitelyTyped/DefinitelyTyped/blob/215cb1970b3c998bac688396c6cd9756b446055b/types/classnames/index.d.ts

christophehurpeau avatar Aug 08 '18 15:08 christophehurpeau