Typescript: Better error message when users mistakenly import from named vs. default export

Created on 20 Jan 2017  路  9Comments  路  Source: microsoft/TypeScript

At google, we've had a number of users trip over import syntax when working with libraries that have default exports.

Given this scenario:

has_named_export.ts

export var a = 1;

has_default_export.ts

var b = '';
export default b;

1) Users often do:

import {b} from './has_default_export';

which gives:
Error: module has no exported member 'b'

Request: the error could be more helpful, like "did you mean to import the default export, with no curly braces"
This is especially likely when the name of the default export matches the named import.
Novice users look at has_default_export and think that B should be importable, and may never have worked with default exports.

2) Also possible:

import a from './has_named_export';

"has_named_export" has no default export

This one is less frequent, but it could say "did you mean to import the named import 'a' by using curly braces"

Bug Error Messages Fixed good first issue help wanted

Most helpful comment

I'd like to give this a shot! This would be my first open source contribution.

All 9 comments

Should be a rather simple check in getExternalModuleMember to add the elaboration. PRs are welcomed.

I'd like to give this a shot! This would be my first open source contribution.

@jules-costa the closed PRs referenced above are fairly close to correct - I'd recommend picking out the good bits from those changes

@jules-costa are you still working on this issue? If not, I would like to give it a try.

@knrt10 Are you still working on this issue? If not, could I take over your PR and update it with the requested fixes?

@joaniedavis I was little busy, but sure go ahead.

I think in general the error messaging for all import errors need an overhaul.

It seems like there are <100 different ways people setup their folder/file/node_modules/vendor folders and determining the correct way to import things when using TypeScript, taking into account issues including whether the script will be used in a Node or Browser environment, is terribly complicated.

My proposal is that any time there is any kind of import/require/triple directive/etc issue, TypeScript examines the user's file system for the <100 most common setups, and provides a suggestion for how they should go about specifying their imports.

Is there something like that underway? I would be happy to put some time into helping.

opening a PR soon! ^^

Thanks @prime-optimus!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Roam-Cooper picture Roam-Cooper  路  3Comments

DanielRosenwasser picture DanielRosenwasser  路  3Comments

CyrusNajmabadi picture CyrusNajmabadi  路  3Comments

Antony-Jones picture Antony-Jones  路  3Comments

weswigham picture weswigham  路  3Comments