microsoft / microsoft/monaco-editor

Can't get @types type definitions to work correctly

Open
#2,549 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug typescript-multifile
Dominant language
JavaScript
Stars
46.8k
Forks
4.1k
Avg merge
17h 58m
Merged PRs (30d)
1

Description

Hi 👋

I'm trying to get typescript definitions working for external libraries, together with peek definition and what not.

So I was playing around with it in the monaco playground, but can't get it to work correctly.

So I pulled down query-string-params from unpkg and try to load them in into Monaco. First of all, when I press the 💡 or Cmd+., it shows the following import path.

image

Off course I want it to just be query-string-params and that it automatically picks up the typescript definitions in @types/query-string-params. But I can't get it to work. If I manually import from 'query-string-params, it does not complain, but it can't find the typescript definitions either. Do I have to manually add @types/query-string-params/index.d.tsasquery-string-params/index.d.ts` instead?

Also, do note the %40 in the path. That is because monaco.Uri.toString() encodes that. If I replace the import to use @, it does not work either.

image

image

So there are a couple of problems I'm experiencing in this demo

  • Path in node_modules is relative
  • @ is being replaced with %40, although, it shouldn't import from that location in the first place
  • Importing the actual library does not work

Thanks in advance 🙏 !

monaco-editor version: 0.25.2
Browser: Chrome
OS: macOS
Playground code that reproduces the issue:

monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
    moduleResolution: monaco.languages.typescript.ModuleResolutionKind.NodeJs,
    module: monaco.languages.typescript.ModuleKind.CommonJS
});

Promise.all([
    fetch('https://unpkg.com/query-string-params@1.7.6/package.json').then((res) => res.text()),
    fetch('https://unpkg.com/query-string-params@1.7.6/dist/index.js').then((res) => res.text()),
    fetch('https://unpkg.com/@types/query-string-params@1.7.0/package.json').then((res) => res.text()),
    fetch('https://unpkg.com/@types/query-string-params@1.7.0/index.d.ts').then((res) => res.text()),
]).then(([pkgJson, source, pkgJsonTypings, typings]) => {
    monaco.languages.typescript.typescriptDefaults.addExtraLib(pkgJson, 'ts:/node_modules/query-string-params/package.json');
    monaco.languages.typescript.typescriptDefaults.addExtraLib(source, 'ts:/node_modules/query-string-params/dist/index.js');
    monaco.languages.typescript.typescriptDefaults.addExtraLib(pkgJsonTypings, 'ts:/node_modules/@types/query-string-params/package.json');
    
    monaco.editor.createModel(typings, 'typescript', monaco.Uri.parse('ts:/node_modules/@types/query-string-params/index.d.ts'));
});

var code = [
	'propertyToUrl()',
].join('\n');

monaco.editor.create(document.getElementById('container'), {
	model: monaco.editor.createModel(code, 'typescript', monaco.Uri.parse('ts:/foo.ts'))
});

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied Monaco playground reproduction and inspect the TypeScript defaults, addExtraLib calls, and ts:/node_modules URIs. Trace module resolution for query-string-params and its @types package; done means imports, definition lookup, and suggested import paths resolve without manual path rewriting.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
developer-experience
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.