monaco-editor
monaco-editor copied to clipboard
Add better support for imports suggestion
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:
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:
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.
Subscribing because this technique is how automatic type acquisition works in the typescript playground
Is there any workaround for these issue ? Because most of @types import is missing explicit module declaration
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
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
.
When imports that are declared via
addExtraLib
are explicitly wrapped insidedeclare 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:
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:
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
I debugged it, it should be caused by https://github.com/microsoft/monaco-editor/issues/2582