TypeScript Compiler API How to set Lib?

Viewed 409

I am using the TypeScript Compiler API which literally has no documentation so hopefully someone can help here.

I'm trying to add in the ES2020 lib option to my compiler options when running ts.createProgram.

I found out that "lib" is a string[] where the CompilerOptions interface is defined here.

I tried setting lib as follows:

const program = ts.createProgram(files, {
    target: ts.ScriptTarget.ES2020,
    module: ts.ModuleKind.ES2015,
    lib: ["ES2020"], // <---
    ...
}, host);

This seems to not be working, I am getting constant compilation errors stating Cannot find name 'Array/Number/Math', etc, so the lib is clearly not being defined correctly.

How can I set the lib correctly???


Edit: I have a custom host object, with the following function which may be relevant to the question:

getDefaultLibFileName(options) {
    return ts.getDefaultLibFilePath(options);
}
2 Answers

it worked for me if I put the entire filename inside the lib field, like;

{
  ...
  lib: ['lib.esnext.d.ts', 'lib.dom.d.ts'],
  ...
}

"I don't have any node_modules in my code that's being compiled." I believe this is the reason it's not working. The host will look at the code's node_modules folder to find the lib.es20202.full.d.ts file.

See the result of ts.getDefaultLibFilePath(options) in the host and I think it will be a file that doesn't exist.

To fix this, ensure the custom host provides the file path to a .d.ts file that exists or have the host provide the text of the file the compiler is searching for (this could be done in memory using the host).

Related