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

How to scope intillisense down to just passed in `.d.ts` defs.

Open philcockfield opened this issue 8 years ago • 7 comments

I’m looking at the configuration options for the VSCode MonacoEditor related to specifying intellisense via .d.ts definitions:

Playground: Configure JS Defaults Example

// compiler options
monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
    target: monaco.languages.typescript.ScriptTarget.ES6,
    allowNonTsExtensions: true
});

// extra libraries
monaco.languages.typescript.javascriptDefaults.addExtraLib([
    'declare class Facts {',
    '    /**',
    '     * Returns the next fact',
    '     */',
    '    static next():string',
    '}',
].join('\n'), 'filename/facts.d.ts');

var jsCode = [
    '"use strict";',
    '',
    "class Chuck {",
    "    greet() {",
    "        return Facts.next();",
    "    }",
    "}"
].join('\n');

monaco.editor.create(document.getElementById("container"), {
    value: jsCode,
    language: "javascript"
});

I’m wondering if there is a way to scope intillisense down to just the types passed in via addExtraLib - at the moment the kitchen sink of ES6 is available in the dropdown lists, including all of HTML5 for the browser etc etc.

For my purposes, I’m having the editor run in a tightly constrained environment so most of the API surface area represented in these dropdowns in not relevant.

The target field of setCompilerOptions seems likely, but in playing around with that it doesn’t seem to have any effect on reducing scope, eg all of these options don't narrow scope at all:

image

ref Playground


Any ideas? It feels like this must be somewhere else in the API, yeah? Thanks 🌵

philcockfield avatar Aug 14 '18 00:08 philcockfield

You can pass 'nolib' option to setcompileroption.

yln99517 avatar Aug 14 '18 03:08 yln99517

Thanks @yln99517 - that's great.

Setting nolib to true seems wipes out the HTML/Browser centric intillisense, and I am just getting my passed in .d.ts.

As a follow-on question, how would I go about dealing with this "No Promise" error:

image

I'm setting noLib and then passing in ES2015 to lib as suggested, but no dice:

    const typescript = monaco.languages.typescript;

    typescript.typescriptDefaults.setCompilerOptions({
      noLib: true, 
      lib: ['ESNext', 'ES2015'],
      target: typescript.ScriptTarget.ESNext,
      allowNonTsExtensions: true,
    });

This has the same effect to (dropping the noLib in favor explicitly passing the lib array, which I'm wildly guessing at being the sameis effect:

    const typescript = monaco.languages.typescript;

    typescript.typescriptDefaults.setCompilerOptions({
      lib: ['ESNext', 'ES2015'],
      target: typescript.ScriptTarget.ESNext,
      allowNonTsExtensions: true,
    });

Any ideas? Thanks!

philcockfield avatar Aug 14 '18 21:08 philcockfield

You need to extract required libs from default libs then pass to addExtraLib. It solves the problem.

yln99517 avatar Aug 15 '18 03:08 yln99517

Thanks @yln99517. Where do I find the "default libs" to extract from?

philcockfield avatar Aug 15 '18 04:08 philcockfield

I wonder if you might be able to give me a brief code sample to get me going in the right direction please @yln99517 ?

philcockfield avatar Aug 16 '18 08:08 philcockfield

Hi , Goto this. You can see available libs or default libs. Pack required libs into one file or in a string. Pass the string to addExtraLib. Hope this helps. thanks

yln99517 avatar Aug 16 '18 11:08 yln99517

@yln99517 - sweet! That's perfect, thanks!

philcockfield avatar Aug 17 '18 08:08 philcockfield