Typescript: @ts-check for .js files with isolatedModules causes 'use strict' to error

Created on 19 Dec 2018  Â·  5Comments  Â·  Source: microsoft/TypeScript


TypeScript Version: 3.2.2


Search Terms: isolatedModules "use strict"

Code

source/index.js:

// @ts-check
'use strict'
module.exports = thing

tsconfig.json:

{
  "compilerOptions": {
    "target": "esnext",
    "moduleResolution": "node",
    "allowJs": true,
    "noEmit": true,
    "strict": true,
    "isolatedModules": true,
    "esModuleInterop": true
  },
  "include": ["source"]
}

VSCode as well as tsc --project . will cause the failure of:

[ts] Cannot compile namespaces when the '--isolatedModules' flag is provided. [1208]

Which I did not expect.

Explanation:
In this case, I am using typescript only for type checking js files.

The full scenario is here: https://github.com/bevry/eachr/tree/c5b650c961c0389a6a9fab175c41be0250a9f560 which uses babel for compiling .js files, and @ts-check for type checking, and eslint for linting.

The reason I have that tsconfig.json file is that it is what is recommended by microsoft — https://blogs.msdn.microsoft.com/typescript/2018/08/27/typescript-and-babel-7/ — when using typescript with babel, which I assume is also what applies when using typescript with .js files that are compiled via babel.

Bug Fixed

Most helpful comment

Just tried out --isolatedModules on my project as well and running into this same issue. In my case I've been running TypeScript across my entire project to also type check things like webpack.config.js. However those files are standard nodejs files that use commonjs exports. It looks like if I changed those files to use es module syntax, the type check error goes away, but of course the file is not usable by the tooling. If there was a way to mark a specific directory as adhering to isolated modules, this would resolve my issue on this.

All 5 comments

Just tried out --isolatedModules on my project as well and running into this same issue. In my case I've been running TypeScript across my entire project to also type check things like webpack.config.js. However those files are standard nodejs files that use commonjs exports. It looks like if I changed those files to use es module syntax, the type check error goes away, but of course the file is not usable by the tooling. If there was a way to mark a specific directory as adhering to isolated modules, this would resolve my issue on this.

Is there some mitigation for this issue? I am getting it when trying to import js files from ts files in a project.

The error is really bad here. It should say something like "All files must be modules when the '--isolatedModules' flag is provided.", and the span should probably not point to the first word of the file.

Unfortunately, this check happens after parsing but before binding. We check for modules during parsing for TS since it's syntactically obvious; we check for modules in binding for JS since it's not. I think the correct fix is not to error on JS at all since we're not going to transform it anyway.

I'll have a PR up shortly.

@sandersn Very nice fix, thank you! :)

Any workaround for previous versions of TS ?

Turn off isolatedModules ? or turn off allowJs ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kyasbal-1994 picture kyasbal-1994  Â·  3Comments

zhuravlikjb picture zhuravlikjb  Â·  3Comments

seanzer picture seanzer  Â·  3Comments

uber5001 picture uber5001  Â·  3Comments

siddjain picture siddjain  Â·  3Comments