TypeFox / monaco-languageclient

Repo hosts npm packages for monaco-languageclient, vscode-ws-jsonrpc, monaco-editor-wrapper, @typefox/monaco-editor-react and monaco-languageclient-examples
https://www.npmjs.com/package/monaco-languageclient
MIT License
1.06k stars 178 forks source link

monaco.languages.typescript is undefined #746

Closed wangtuyao closed 1 month ago

wangtuyao commented 1 month ago

Hi,

I am using monaco-languageclient to connect to a typescript language server backend service. The workspace path points to a typescript project, and it is successful. IntelliSense is also working properly.

However, I am unable to use monaco.languages.typescript.javascriptDefaults.addExtraLib to add some extra in-memory created libraries because monaco.languages.typescript is undefined.

I noticed in #735 that it requires import @codingame/monaco-vscode-standalone-typescript-language-features. After trying that, I found that monaco.languages.typescript.javascriptDefaults.addExtraLib could be successfully called, but another error appeared :

errors.js:10 Uncaught Error: Unexpected usage

Error: Unexpected usage
    at _EditorSimpleWorker.loadForeignModule (editorSimpleWorker.js:583:33)
    at webWorker.js:29:30
    at async tsMode.js:86:16
    at async WorkerManager.getLanguageServiceWorker (tsMode.js:92:20)
    at async DiagnosticsAdapter._doValidate (tsMode.js:377:20)
    at _EditorSimpleWorker.loadForeignModule (editorSimpleWorker.js:583:33)
    at webWorker.js:29:30
    at async tsMode.js:86:16
    at async WorkerManager.getLanguageServiceWorker (tsMode.js:92:20)
    at async DiagnosticsAdapter._doValidate (tsMode.js:377:20)
    at errors.js:10:29

My question is:

  1. How can be resolved this error?
  2. Since my typescript language client workspace points to a typescript project (in order to obtain IntelliSense for this project), but I also use monaco.languages.typescript.javascriptDefaults.addExtraLib to add extra libraries in memory, how does Monaco's IntelliSense behave in this case? Does it prioritize the content from monaco.languages.typescript.javascriptDefaults.addExtraLib or the language client? If there are conflicting types with the same name, how does IntelliSense handle the suggestions?

Thinks.

kaisalmen commented 1 month ago

Hi @wangtuyao which typescript language server backend service do you use? If you use a language server you can use the syntax highlighting in the front-end, but you should not use the standalone worker services for TypeScript that monaco-editor provides. Extra library loading should be handle by the language server in your scenario as far as I can tell.

wangtuyao commented 1 month ago

@kaisalmen Thinks for reply.

If you use a language server you can use the syntax highlighting in the front-end, but you should not use the standalone worker services for TypeScript that monaco-editor provides

Does this mean that once I use typescript-language-server to get syntax highlighting and IntelliSense on brower, I won't be able to use standalone worker services e.g. monaco.languages.typescript.javascriptDefaults.addExtraLib to add some extra lib in memory?

kaisalmen commented 1 month ago

Does this mean that once I use typescript-language-server to get syntax highlighting and IntelliSense on browser, I won't be able to use standalone worker services

@wangtuyao I don't know. How do they configure that in VSCode? Do they have examples for that?

kaisalmen commented 1 month ago

Btw, have you checked https://github.com/TypeFox/monaco-languageclient/blob/main/packages/examples/src/ts/wrapperTs.ts This example makes uses of VSCode typescript support and runs it in a worker. Maybe this is can be more helpful for you use case? It does not require to have a backend.

CGNonofr commented 1 month ago

monaco.languages.typescript.javascriptDefaults.addExtraLib is only used to configure the monaco typescript worker

There are 3 ways to provide typescript intellisense:

You shouldn't use more than one or you'll have duplicated suggestions

I'm not sure why you may want to call addExtraLib if you use a remote language server, you can install types just as you would do in any project

wangtuyao commented 1 month ago

@CGNonofr @kaisalmen thinks. got it . In our case we should use monaco worker.