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.

贡献者指南