Provide type hints to monaco editor

后端 未结 3 2091
耶瑟儿~
耶瑟儿~ 2021-02-10 12:34

I am trying to provide intellisense / code completion into a javascript editor using the Monaco editor. The code needs to be valid javascript, not typescript.

Given some

3条回答
  •  醉梦人生
    2021-02-10 12:59

    As of Monaco version 0.90, since https://github.com/Microsoft/monaco-editor/issues/203 has been fixed, you can add achieve this partially if you use JSDoc in the editing code.

    For this code in the left side of the Monaco playgound:

        // validation settings
    monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({
        noSemanticValidation: true,
        noSyntaxValidation: false
    });
    
    // compiler options
    monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
        target: monaco.languages.typescript.ScriptTarget.ES6,
        allowNonTsExtensions: true,
        allowJs: true
    });
    
    // extra libraries
    monaco.languages.typescript.javascriptDefaults.addExtraLib([
        'declare class SomeEventType {',
        '    /**',
        '     * Heres the doco for someProperty',
        '     */',
        '    someProperty: string',
        '}',
    ].join('\n'), 'filename/facts.d.ts');
    
    var jsCode = [
        '"use strict";',
        '',
        "/**",
        " * @param {SomeEventType} event",
        " */",
        "function onMyEvent(event) {",
        "",
        "}"
    ].join('\n');
    
    monaco.editor.create(document.getElementById("container"), {
        value: jsCode,
        language: "javascript"
    });
    

    Means that the editor can now interpret the event parameter as a SomeEventType:

提交回复
热议问题