monaco-editor icon indicating copy to clipboard operation
monaco-editor copied to clipboard

Add better support for imports suggestion

Open ErkoKnoll opened this issue 4 years ago • 6 comments

When imports that are declared via addExtraLib are explicitly wrapped inside declare module then IntelliSense is actually very nice. Following setup:

monacoEditor.languages.typescript.typescriptDefaults.addExtraLib("declare module 'test/file1' { export interface Test {} }", 'file:///node_modules/@types/test/file1.d.ts')
monacoEditor.languages.typescript.typescriptDefaults.addExtraLib("declare module 'test/file2' { export interface Test {} }", 'file:///node_modules/@types/test/file2.d.ts')

produces very nice auto-completion: Screenshot 2020-02-18 at 16 57 04 Screenshot 2020-02-18 at 16 57 12

But if type definitions are missing explicit module wrapping as following:

monacoEditor.languages.typescript.typescriptDefaults.addExtraLib('export interface Test {}', 'file:///node_modules/@types/test/file1.d.ts')
monacoEditor.languages.typescript.typescriptDefaults.addExtraLib('export interface Test {}', 'file:///node_modules/@types/test/file2.d.ts')

then IntelliSense is also completely missing: Screenshot 2020-02-18 at 16 58 10

It would be nice if the latter use case would also produce the same outcome as the first one without needing to explicitly wrap type definitions.

ErkoKnoll avatar Feb 18 '20 15:02 ErkoKnoll

Subscribing because this technique is how automatic type acquisition works in the typescript playground

orta avatar Feb 20 '20 22:02 orta

Is there any workaround for these issue ? Because most of @types import is missing explicit module declaration

rizrmd avatar Jul 22 '20 09:07 rizrmd

I spent a bit of time digging through old Stack Overflow answers (example), and adding the declare module 'package-name' seemed to be the missing piece here. So, thanks for this issue, and it would be great to (a) feature an example like this in the monaco editor playground, and (b) perhaps display clearer error messages when extra libs are registered in un-usable ways. I've written up the solution I finally ended up with, here: https://stackoverflow.com/a/66948535

donmccurdy avatar Apr 05 '21 05:04 donmccurdy

I found a solution. Just add a simple package.json and specify dependencies.

.addExtraLib(,"file:///node_modules/foo/index.d.ts")
.addExtraLib(,"file:///node_modules/foo/bar/index.d.ts")

const packageJson = {dependencies:{
  "foo": "*",
  "foo/bar": "*",
}}

.addExtraLib(JSON.stringify(packageJson), "package.json")

No need to bundle all type definitions or wrap definitions with declare module.

wirekang avatar Jan 27 '24 14:01 wirekang

When imports that are declared via addExtraLib are explicitly wrapped inside declare module then IntelliSense is actually very nice. Following setup:

monacoEditor.languages.typescript.typescriptDefaults.addExtraLib("declare module 'test/file1' { export interface Test {} }", 'file:///node_modules/@types/test/file1.d.ts')
monacoEditor.languages.typescript.typescriptDefaults.addExtraLib("declare module 'test/file2' { export interface Test {} }", 'file:///node_modules/@types/test/file2.d.ts')

produces very nice auto-completion: Screenshot 2020-02-18 at 16 57 04 Screenshot 2020-02-18 at 16 57 12

But if type definitions are missing explicit module wrapping as following:

monacoEditor.languages.typescript.typescriptDefaults.addExtraLib('export interface Test {}', 'file:///node_modules/@types/test/file1.d.ts')
monacoEditor.languages.typescript.typescriptDefaults.addExtraLib('export interface Test {}', 'file:///node_modules/@types/test/file2.d.ts')

then IntelliSense is also completely missing: Screenshot 2020-02-18 at 16 58 10

It would be nice if the latter use case would also produce the same outcome as the first one without needing to explicitly wrap type definitions.

Why isn't the autocomplete working even when I use the first method? See playgroud link

xiaoxiyao avatar Mar 05 '24 11:03 xiaoxiyao

I debugged it, it should be caused by https://github.com/microsoft/monaco-editor/issues/2582

xiaoxiyao avatar Mar 06 '24 06:03 xiaoxiyao