typescriptfetch-apiabortcontroller

AbortController missing in TypeScript


I am trying to use AbortController in TypeScript.

Given this small file:

const controller = new AbortController();

I get the following error from TypeScript compiler:

src/testAbort.ts:1:24 - error TS2304: Cannot find name 'AbortController'.

1 const controller = new AbortController();
                         ~~~~~~~~~~~~~~~

TypeScript has documentation about AbortController. I also found an issue from Github which has been resolved by merging a pull request that contains the type definitions for AbortController. So it should be available.

My tsconfig.json contains:

{
  "compilerOptions": {
    "target": "ES2018",
    "lib": ["ES2018"],
    "allowSyntheticDefaultImports": true,
    "moduleResolution": "node",
    "baseUrl": "src"
  },
  "exclude": ["node_modules"],
  "include": ["src/**/*", "__tests__/**/*", "index.ts"],
  "typeRoots": ["./node_modules"]
}

What I have tried:


Solution

  • It's because you are missing the value DOM in the lib array of your tsconfig.json. If you check the official repo, you will find the AbortController here!