Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why declaration can not be used together with isolatedModules in typescript?

Tags:

typescript

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'.
like image 312
Leon Avatar asked Mar 21 '18 10:03

Leon


People also ask

What is isolatedModules TypeScript?

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 the purpose of a TypeScript declaration file?

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.

Can't find a declaration file for module Morgan?

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 .


1 Answers

Answer

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.

Suggested Workaround

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
    }
}
like image 158
Shaun Luttin Avatar answered Sep 20 '22 01:09

Shaun Luttin