Typescript: More accurate error message when re-exporting a type with --isolatedModules

Created on 29 Jul 2019  路  5Comments  路  Source: microsoft/TypeScript

Search Terms

--isolatedModules, re-export

Suggestion

Provide guidance to the user on what to do when they hit the "Cannot re-export a type when the '--isolatedModules' flag is provided" error message in cases like this:

export { Foo } from "./mod";

As-is, the error message:

  • doesn't tell the user what the (non-obvious) workarounds are
  • is incorrect because it is possible for re-export types - the user just needs to use alternative syntax

Here are some options for fixing:

  1. "Try rewriting as export type Foo = import('./mod').Foo
  2. "Try rewriting as import { Foo as FooAlias } from "./mod"; export type Foo = FooAlias;"

Use Cases

Currently, we've seen that users don't know what action to take when they hit this error message. Adding this additional information will provide clear guidance to the user on the preferred workaround.

Examples

Code:

export { Foo } from "./mod";

Full error message:

" Try rewriting as export type Foo = import('./mod').Foo. Reason: When the '--isolatedModules' flag is provided, TS must be able to distinguish types and values by looking at only the current file.`

Checklist

My suggestion meets these guidelines:

  • [x ] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [x] This wouldn't change the runtime behavior of existing JavaScript code
  • [x] This could be implemented without emitting different JS based on the types of the expressions
  • [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [x] This feature would agree with the rest of TypeScript's Design Goals.
Awaiting More Feedback Suggestion

All 5 comments

I feel like the right answer is to just make a Stack Overflow Q&A about this with more examples and explanation. The error message is very to-the-point and we don't typically dive super deep into the "why" or "what else should I do" unless we can deliver a one-line alternative.

Thanks for explaining.

Isn't the current error message incorrect, though? It says that re-exporting a type isn't permitted. But re-exporting a type is permitted, provided the user aliases or uses different syntax.

@mheiber I agree that this is a misleading error message. At the very least it should say Cannot export unaliased types when '--isolatedModules' flag is provided. That would at the very least give you the hint about aliasing.

We shouldn't have to go to SO just to decipher what every esoteric message typescript provides means when there are useful and friendly alternatives. It makes the language very inaccessible.

In TS 3.8 the message is Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'., so this issue can be closed?

In TS 3.8 the message is Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'., so this issue can be closed?

馃憤 thanks for the reminder, @vkrol

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jbondc picture jbondc  路  3Comments

blendsdk picture blendsdk  路  3Comments

Zlatkovsky picture Zlatkovsky  路  3Comments

Antony-Jones picture Antony-Jones  路  3Comments

manekinekko picture manekinekko  路  3Comments