declaration is used to generate type file, and isolatedModules mean all file should be a separate module. Why do these two options use together?
error TS5053: Option 'declaration' cannot be specified with option 'isolatedModules'.
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.
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.
Declaration files, if you're not familiar, are just files that describe the shape of an existing JavaScript codebase to TypeScript. By using declaration files (also called . d. ts files), you can avoid misusing libraries and get things like completions in your editor.
The error "Could not find declaration file for module" occurs when TypeScript cannot find the type declaration for a module. To solve the error, install the types for the module by running the command from the error message, e.g. npm install -D @types/module-name .
In the issue that Max Heiber referenced in the comments, Wesley Wigham from the TypeScript team posted the following answer (emphasis added):
The reason is for the same reason you can't use const enums in isolated modules: type information. Since isolated modules compiles each file individually without the types of the files it depends on, any inferred type we would write to a declaration file would potentially be incorrect, as their calculation would be missing information from the rest of the compilation. There is a limited subset of declaration emit where no types are inferred in the output which could be supported, however.
In other words, isolatedModules
does not provide enough type information for the creation of complete and accurate *.d.ts
declaration files.
The issue comments also have a suggested workaround, in which we have one tsconfig
for compiling with isolated modules, and a second tsconfig for creating declaration files.
tsconfig.json
{
"compilerOptions": {
"incremental": true,
"isolatedModules": true
}
}
tsconfig-for-declarations.json
{
"extends": "./tsconfig",
"compilerOptions": {
"emitDeclarationOnly": true,
"isolatedModules": false,
"declaration": true
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With