5

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);
}
5
  • What does host look like? Perhaps that's not searching in the correct place for node_modules. Commented Aug 6, 2020 at 16:10
  • @DavidSherret I don't have any node_modules in my code that's being compiled. Anyway I'm trying to fix language built-ins such as Math, not anything from a node module. Commented Aug 6, 2020 at 16:31
  • @DavidSherret I have a custom host, I've updated the question to show something that might be relevant. Commented Aug 6, 2020 at 16:34
  • curious what you need to do that you can't do with tsc Commented Aug 6, 2020 at 16:36
  • @inorganik custom module resolution, and rewriting imports :) Commented Aug 6, 2020 at 16:41

2 Answers 2

4

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

{
  ...
  lib: ['lib.esnext.d.ts', 'lib.dom.d.ts'],
  ...
}
Sign up to request clarification or add additional context in comments.

Comments

-1

"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).

4 Comments

Wow, that makes a lot of sense, I'll try that! :)
What do you mean by the host's node_modules folder? I still have a node_modules folder along with my code that uses the compiler API, but the code that is being compiled does not have a node_modules folder... so I think it should actually still work. I tried updating the function to return the exact location to the lib file lib.es2020.full.d.ts and that hasn't fixed anything.
@DavidCallanan I misremembered where it gets the lib files from, but yes you are right that it will use the current compiler's node_module's folder. Check the calls the compiler makes to all the readFile and fileExists on the host and ensure those files exist. If that doesn't work, then I'm not sure what's wrong without seeing more code.
Something is fishy... if I remove the lib option entirely, but keep my getDefaultLibFileName function the same (returning the lib.es2020 file), the errors go away. Hopefully that stays working... thanks for all the help anyways!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.