monaco-editor icon indicating copy to clipboard operation
monaco-editor copied to clipboard

[Bug] Using incorrect lib in setCompilerOptions causes unexpected autocomplete failures

Open camargo opened this issue 3 years ago • 1 comments

Reproducible in vscode.dev or in VS Code Desktop?

  • [X] Not reproducible in vscode.dev or VS Code Desktop

Reproducible in the monaco editor playground?

Monaco Editor Playground Code

const content = `export type Args =
| { foo: number, bar: number }
| { foo: number }
| { bar: number };

declare global {
  class FooBar {
    public static foo(opts: { args: Args }): void
  }
}
`

const options = monaco.languages.typescript.typescriptDefaults.getCompilerOptions();
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({ ...options, lib: ['ESNext'] });
monaco.languages.typescript.typescriptDefaults.setExtraLibs([{ content, filePath: 'test.d.ts' }]);

monaco.editor.create(document.getElementById('container'), {
  value: `FooBar.foo({ args: {} })`,
  language: 'typescript'
});

Reproduction Steps

After loading the above code in the playground, move the cursor into the args: {} object and trigger autocomplete with ctrl+shift.

Actual (Problematic) Behavior

Upon triggering autocomplete you will not see the foo or bar properties available in the autocomplete popup.

However if you change the playground code content variable to:

const content = `export type Args = { foo: number, bar: number };

declare global {
  class FooBar {
    public static foo(opts: { args: Args }): void
  }
}
`

The autocomplete works as expected.

Expected Behavior

We expect the autocomplete to work in both cases. setCompilerOptions should complain about invalid lib values.

Additional Context

@adrienmaillard and I worked around this problem in our project by using the correct lib string in setCompilerOptions:

monaco.languages.typescript.typescriptDefaults.setCompilerOptions({ ...options, lib: ['esnext'] });

A possible path to helping with this is to strongly type the lib property in CompilerOptions. The TypeScript documentation on lib uses uppercase which led us to this issue in the first place.

camargo avatar Aug 02 '22 17:08 camargo

TypeScript hits pathForLibFile with ESNext. Then path is undefined.

This sounds like a bug in TypeScript.

hediet avatar Aug 03 '22 15:08 hediet