Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot import module in v1.1.0 (like one could with 1.0.1) #3

Closed
mattbishop opened this issue May 15, 2018 · 7 comments
Closed

Cannot import module in v1.1.0 (like one could with 1.0.1) #3

mattbishop opened this issue May 15, 2018 · 7 comments

Comments

@mattbishop
Copy link

mattbishop commented May 15, 2018

Before today's upgrade, I could use the es6-style imports in TypeScript:
import * as deepFreeze from "deep-freeze-es6";

Now I get this error:


10 import * as deepFreeze from "deep-freeze-es6";

I can fix it by switching to require, but that feels so old-fashioned now:
import deepFreeze = require("deep-freeze-es6");

@christophehurpeau
Copy link
Owner

christophehurpeau commented May 15, 2018

does import deepFreeze from "deep-freeze-es6"; works ? (it does with babel 7 and @babel/preset-typescript, but I'm not that familiar with typescript)

@mattbishop
Copy link
Author

mattbishop commented May 15, 2018

No it does not work in TS. I get the following error: error TS1192: Module '"/Users/mbishop/OCTO/atlantis/node_modules/deep-freeze-es6/index"' has no default export.

@christophehurpeau
Copy link
Owner

I think it's related to this: microsoft/TypeScript-React-Starter#8
I do have "allowSyntheticDefaultImports": true in my tsconfig.json, so that explains why it works for me. Would that work for you ?

@mattbishop
Copy link
Author

Fair enough. It looks like an incompatibility between es6 modules and typescript.

FWIW I added this a while back: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/deep-freeze-es6/index.d.ts

Perhaps your type definition file can be reworked?

@christophehurpeau
Copy link
Owner

Sorry for the delay of response...

I'm not sure I want to support import * as deepFreeze from "deep-freeze-es6";: it feels wrong, because it should mean that you have all exported variables/functions in deepFreeze and should be accesible with deepFreeze.a, deepFreeze.b etc

https://www.typescriptlang.org/docs/handbook/modules.html

Import the entire module into a single variable, and use it to access the module exports

import * as validator from "./ZipCodeValidator";
let myValidator = new validator.ZipCodeValidator();

So it doesn't really make sense to have a function if you do a import * as. What do you think ?

@mattbishop
Copy link
Author

If I look at how I'm importing other TS libs (those either written in TS or with @types declarations) they all follow the pattern import * as thing from "thing" regardless of their exported content. Most export more than a single function, to be fair. For a regular developer as myself, I like pattern consistency. When something "sticks out" like this, I wonder if I'm doing something wrong, or is there something wrong with the lib, or something else.

Also, tuning up the tsconfig.json file exacerbates this feeling, like I have to work around something the author didn't get right.

One lib to consider following is chalk. It exports a single default interface and you import it with import chalk from "chalk";. To accomplish this with DeepFreeze the type declaration file would be:

declare function deepFreeze<T>(obj: T): T;
export default deepFreeze;

Then one would import it with import deepFreeze from "deepFreeze"; No require() required :)

@christophehurpeau
Copy link
Owner

I can only use export default deepFreeze; if I use babel to compile the file to support both module.exports and module.exports.default, or implements this myself in the lib. I didn't want to add this complexity in this lib, but I think it's the most easier solution : https://github.com/chalk/chalk/blob/master/index.js#L228

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants