Getting Error: "All files must be modules when the '--isolatedModules' flag is provide" in React with TypeScript
I can't seem to get rid of this error. I'am using create-react-app with TypeScript. Any help, appreciated. Thanks.
All files must be modules when the '--isolatedModules' flag is provided. TS1208
> 1 | import randomcolor from 'randomcolor';
| ^
2 | import React from 'react';
3 | import tasksData from '../../assets/data/tasksData';
4 | import { Task } from '../../model/task';
These are my tsconfig files. I created a base file as the original tsconfig file gets overwritten by the react build scripts.
tsconfig.base.json
{
"compilerOptions": {
"isolatedModules": false
}
}
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": [
"src"
],
"exclude": [
"node_modules"
],
"extends": "./tsconfig.base.json"
}
May get this error with an unused .ts
or .tsx
file. I had empty .ts
file I wasn't using so just had to delete the unused file.
deleted the unused file /example/index.ts
The problem is that in Typescript by default, every file without import
or export
is a legacy scrip file.
As such files are not modules they are merged in the global namespace.
To change this behavior
a) add any import or export in the file, eg: export {}
b) set "isolatedModules": false
in tsconfig.json
Note: NextJS will reset the value to true
in every build, because for NextJS, "isolatedModules": true
is a requirement