create-react-context
create-react-context copied to clipboard
fix TypeScript typings exports
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.
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.
Failure or not (yeah, it's a workaround that TypeScript upholds) - currently it just does not work:)
Workaround in every package is broken ecosystem.
Here to confirm the problem. The library can not be consumed by a Typescript project as of now.
Can't you play with some options to solve the problem on your side?
Example to support all options : https://github.com/DefinitelyTyped/DefinitelyTyped/blob/215cb1970b3c998bac688396c6cd9756b446055b/types/classnames/index.d.ts