Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

window.MonacoEnvironment isn’t defined in types. #2580

Closed
remcohaszing opened this issue Jul 18, 2021 · 6 comments · Fixed by microsoft/vscode#128949
Closed

window.MonacoEnvironment isn’t defined in types. #2580

remcohaszing opened this issue Jul 18, 2021 · 6 comments · Fixed by microsoft/vscode#128949
Milestone

Comments

@remcohaszing
Copy link
Contributor

monaco-editor version: 0.26.1
Browser: N/A
OS: N/A
Playground code that reproduces the issue: N/A

The following TypeScript code:

window.MonacoEnvironment = {};

Yields:

Property 'MonacoEnvironment' does not exist on type 'Window & typeof globalThis'.

window.MonacoEnvironment isn’t defined by the type definitions, only the global MonacoEnvironment is.

@hediet
Copy link
Member

hediet commented Jul 23, 2021

Thanks for the issue & the PR!
Why is the fix for #1795 not working?

@remcohaszing
Copy link
Contributor Author

The type of window isn’t the same as global.

The PR sets the type of the global MonacoEnvironment, so the following works:

const foo = MonacoEnvironment;

However, a typical use case is to set the global MonacoEnvironment by assigning it to window. This currently requires to manually define the type, because it’s missing from Window.

declare global {
  interface Window {
    MonacoEnvironment: Environment;
  }
}

window.MonacoEnvironment = {
  getWorker(workerId, label) {
    switch (label) {
      case 'css':
        return new CssWorker();
      case 'editorWorkerService':
        return new MonacoWorker();
      case 'json':
        return new JsonWorker();
      case 'yaml':
        return new YamlWorker();
      default:
        throw new Error(`Unknown label ${label}`);
    }
  },
};

https://github.com/appsemble/appsemble/blob/d4998c7b7bb042ed8c1d035e76df94ffdfa02c0b/packages/studio/src/components/MonacoEditor/custom.ts#L23-L44

@hediet hediet added this to the August 2021 milestone Jul 23, 2021
@hediet
Copy link
Member

hediet commented Jul 23, 2021

Sounds reasonable. But I would like to hear @alexdima's opinion on this before merging the PR.
He is on vacation though.

@remcohaszing
Copy link
Contributor Author

Another approach I’d personally like even better, is to get rid of the global MonacoEnvironment altogether and configure this using a function.

import { setMonacoEnvironment } from 'monaco-editor/esm/vs/editor/editor.api';
// Both imports should work
// import { setMonacoEnvironment } from 'monaco-editor';

setMonacoEnvironment({
  getWorker(workerId, label) {
    switch (label) {
      case 'css':
        return new CssWorker();
      case 'editorWorkerService':
        return new MonacoWorker();
      case 'json':
        return new JsonWorker();
      case 'yaml':
        return new YamlWorker();
      default:
        throw new Error(`Unknown label ${label}`);
    }
  },
});

@remcohaszing
Copy link
Contributor Author

I like #2605 even better than my solution in the last comment.

@remcohaszing
Copy link
Contributor Author

The types that have been added in microsoft/vscode#128949 are incorrect. Due to how the types are processed, the global Window interface isn’t augmented.

/cc @alexdima

@github-actions github-actions bot locked and limited conversation to collaborators Jan 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants