How do I solve "Cannot find name 'Record'" (or other utility types) when using Typescript with JSDoc?

848 views Asked by At

Is it possible to use the Utility types, such as Record when using JSDoc + tsserver in plain JavaScript?

(I'm trying to create a setup that gives me as many of the gains of TypeScript as I can stand, without the transpiling)

Sample Code

I happen to actually need a map, which I understand is defined in TypeScript as "Record":

let http = require("http");

/**@type {Record<string, Person>} */
let people = {
  "1234": { name: "Bob" },
  "abcd": { name: "Jane" }
};

http.createServer(function (req, res) {
  res.end(JSON.stringify(people));
}).listen(process.env.PORT || 3000);

Error

However, when I try to use it I get this error:

[tsserver] Cannot find name 'Record'. [E]

Config

Version:

tsc --version:

Version 4.3.5

tsconfig.json:

{
  "compilerOptions": {
    "incremental": true,
    "target": "ESNEXT",
    "module": "commonjs",
    "lib": ["DOM"],
    "allowJs": true,
    "checkJs": true,
    "tsBuildInfoFile": "./cache",
    "noEmit": true,
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictBindCallApply": true,
    "strictPropertyInitialization": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": false,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": true,
    "moduleResolution": "node",
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["**/*.js"],
  "exclude": ["node_modules"]
}

For reference, this is how I set ale to use tsserver in my .vimrc:

let g:ale_linters = {'javascript': ['tsserver', 'jshint']}
1

There are 1 answers

2
yume_chan On BEST ANSWER

Record type is defined in lib.es5.d.ts so you need to add at least es5 into your lib array.

If you don't set lib, it defaults to target, but if you set it, basic es libs must be manually included (commonly used for environments with polyfills, but don't support new language features).