TypeScript: Jquery error TS1209: Ambient const enums are not allowed when the '--isolatedModules' flag is provided.

TypeScript Version: 2.6.2

Code

//package.json:
{
  "name": "issue-ts-isolatedmodule",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "jquery": "^3.2.1"
  }
  ,"devDependencies": {
    "typescript": "^2.6.2",
    "@types/jquery":"^3.2.17"
  }
}

//tsconfig.json

{
  "compilerOptions": {
    "outDir": "build/dist",
    "module": "esnext",
    "target": "es5",
    "lib": ["es6", "dom"],
    "sourceMap": true,
    "allowJs": true,
    "jsx": "react",
    "moduleResolution": "node",
    "rootDir": "src",
    "forceConsistentCasingInFileNames": true,
    "suppressImplicitAnyIndexErrors": true,
    "isolatedModules": true,
    "preserveConstEnums": true,
    "preserveSymlinks": true,
    "allowSyntheticDefaultImports": true
  },
  "exclude": [
    "node_modules",
    "build",
    "scripts",
    "acceptance-tests",
    "webpack",
    "jest",
    "src/setupTests.ts"
  ]
}
//src/index.ts
export function HalloWorld(){
    return "Hallo world!"
}

Executing tsc -p ..

Expected behavior: successfully compiled /build/dist/{tsfilename}.js for each ts module:

Actual behavior:

node_modules/@types/jquery/index.d.ts(7690,16): error TS1209: Ambient const enums are not allowed when the '--isolatedModules' flag is provided.
node_modules/@types/jquery/index.d.ts(7698,16): error TS1209: Ambient const enums are not allowed when the '--isolatedModules' flag is provided.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 22 (15 by maintainers)

Commits related to this issue

Most upvoted comments

I have the same error with chalk and @types/range-parser in node_modules. I cannot change them, and use babel with @babel/preset-typescript, any plans on fixing this ? How can I help ?

@alangpierce @chicoxyzzy I solved the above issue by setting skipLibCheck as true in tsconfig.json based on ianschmitz’s suggestion.

https://github.com/facebook/create-react-app/issues/5738

We need to expand the lint rule on DefinitelyTyped as const enum on DT really does not make sense.

Separately, accepting PRs to move the error from a const enum existing to a const enum being referenced (in a value position) when isolatedModules is on. Should be relatively easy.

I put up a PR to change the error to happen when accessing an ambient const enum rather than declaring one (https://github.com/Microsoft/TypeScript/issues/20703#issuecomment-361434795), which should pretty much make this a non-issue: #28465.

@zspitz

Disabling the no-const-enum rule does not solve this problem:

Failed to compile.

/Users/noel/react-auth-demo/node_modules/monaco-editor/esm/vs/editor/editor.api.d.ts h-demo/src/containType error: Ambient const enums are not allowed when the ‘–isolatedModules’ flag is provided. TS1209

I have created a dtslint issue to ban const enums in declaration files. i think a declaration file in general should be applicable to the strictest settings, and this includes --isolatedModules.

I was able to resolve this myself by applying the change I made in this PR with patch-package to override the official @types/ package. This approach can be used to work around the issue for other libraries with outdated types too.