Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is --isolatedModules error fixed by any import?

Tags:

typescript

People also ask

What does isolatedModules do?

Setting the isolatedModules flag tells TypeScript to warn you if you write certain code that can't be correctly interpreted by a single-file transpilation process. It does not change the behavior of your code, or otherwise change the behavior of TypeScript's checking and emitting process.

Can not be compiled under isolatedModules?

The error "Cannot be compiled under '--isolatedModules' because it is considered a global script file" occurs when we have a file in our project that doesn't contain an import or export statement. To solve the error, add an export {} line to the file to make it an ES module.

What is declare module in TypeScript?

The TypeScript declares module is one of the modules and keyword it is used for to surround and define the classes, interfaces; variables are also declared it will not originate with the TypeScript like that module is the set of files that contains values, classes, functions/methods, keywords, enum all these contains ...

What is export type in TypeScript?

TypeScript supports export = to model the traditional CommonJS and AMD workflow. The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum.


Typescript treats files without import/exports as legacy script files. As such files are not modules and any definitions they have get merged in the global namespace. isolatedModules forbids such files.

Adding any import or export to a file makes it a module and the error disappears.

Also export {} is a handy way to make a file a module without importing anything.


The correct way is to tell TypeScript what you want. If you don't want isolatedModules create tsconfig.json inside your test directory and add:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "isolatedModules": false
  },
}

Adding "isolatedModules": true to the config and then cheating TypeScript checker by adding empty export {} smells bad code to me.


Still having error despite you exporting things from that "error file"?

  1. Check if you don't export same name that you already export in another file (conflict)
  2. After your fix try to stop and start your npm/yarn runner (I experienced it cannot recover itself even after hard reload of the page especially when you have another error somewhere else)

Let's try to check isolated modules. When I checked Google, there is no direct context of it.

It basically means that you allow Typescript to compile modules in isolation.

But it comes from Typescript and has something to do with Typescript preferring modules over namespaces.

Modules also have a dependency on a module loader (such as CommonJs/Require.js) or a runtime which supports ES Modules. Modules provide for better code reuse, stronger isolation and better tooling support for bundling.

Source 1

Using a create-react-app typescript project, you should have installed typescript and ts-jest (or the create-react-app should handle the dependencies based on wether you ejected the app or not).

Also ts-jest has some information about it:

By default ts-jest uses TypeScript compiler in the context of a project (yours), with full type-checking and features. But it can also be used to compile each file separately, what TypeScript calls an ‘isolated module’. That’s what the isolatedModules option (which defaults to false) does.

Source 2

As soon as you use the export command you are creating a module out of what is being exported.

If you are using ts-jest, you can add these settings without affecting your other modules, which the create-react-app will consist off.

"ts-jest": {
  "isolatedModules": false
}

And checkout the ts-jest page (second source) for the pro's and con's.