Microsoft/monaco-editor

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

Open

#3,225 建立於 2022年8月2日

在 GitHub 查看
 (1 留言) (0 反應) (0 負責人)JavaScript (1,283 fork)batch import
bughelp wantedtypescriptupstream

倉庫指標

Star
 (14,836 star)
PR 合併指標
 (平均合併 6小時 26分鐘) (30 天內合併 15 個 PR)

描述

Reproducible in vscode.dev or in VS Code Desktop?

  • 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.

貢獻者指南