microsoft / monaco-editor

A browser based code editor
https://microsoft.github.io/monaco-editor/
MIT License
40.36k stars 3.59k forks source link

Monaco-editor-core cannot be compiled: Cannot redeclare block-scoped variable 'MonacoEnvironment'. #4049

Open cuonghuunguyen opened 1 year ago

cuonghuunguyen commented 1 year ago

Does this issue occur when all extensions are disabled?: Yes/No

Steps to Reproduce:

  1. Set up a project like https://github.com/microsoft/monaco-editor/tree/main/samples/browser-esm-webpack-typescript
  2. Perform type check by tsc or fork-ts-checker-webpack-plugin.

Expected behavior Compile successfully

Actual behavior Cannot compile due to the error

node_modules/monaco-editor/esm/vs/editor/editor.api.d.ts:12:9 - error TS2451: Cannot redeclare block-scoped variable 'MonacoEnvironment'.

12     let MonacoEnvironment: Environment | undefined;
           ~~~~~~~~~~~~~~~~~

  node_modules/monaco-editor/monaco.d.ts:11:13
    11 declare let MonacoEnvironment: monaco.Environment | undefined;
                   ~~~~~~~~~~~~~~~~~
    'MonacoEnvironment' was also declared here.

node_modules/monaco-editor/monaco.d.ts:11:13 - error TS2451: Cannot redeclare block-scoped variable 'MonacoEnvironment'.

11 declare let MonacoEnvironment: monaco.Environment | undefined;
               ~~~~~~~~~~~~~~~~~

  node_modules/monaco-editor/esm/vs/editor/editor.api.d.ts:12:9
    12     let MonacoEnvironment: Environment | undefined;
               ~~~~~~~~~~~~~~~~~
    'MonacoEnvironment' was also declared here.

Found 2 errors.

The reason behind this is you are coping declaration file in monaco.d.ts to editor.api.d.ts in https://github.com/microsoft/vscode/blob/c248f9ec0cf272351175ccf934054b18ffbf18c6/build/gulpfile.editor.js#L303-L320

Please fix it so we can compile the apps that use monaco-editor-core

hediet commented 1 year ago

I only get this error in the editor when I open both d.ts files, but not from tsc or webpack.

cuonghuunguyen commented 1 year ago

did you use skipLibCheck for sth that ignore the error? if not could you show me the example how you archive that?

hediet commented 1 year ago

samples/browser-esm-webpack-typescript

I tried this one

cuonghuunguyen commented 1 year ago

Hi @hediet , after investigating, we found out that we have used the monaco.d.ts for the global monaco type by reference /// <reference path="../node_modules/monaco-editor/monaco.d.ts" />

However, I think this is still an issue if we want to have typesafe with monaco global when using monaco loader. And there are no examples of doing that in your repo. If you consider this to be a bug, I can change the title of this issue.