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

How to enforce function signatures in JavaScript?

Open AlCalzone opened this issue 8 years ago • 2 comments

monaco-editor version: 0.13.1 Browser: Firefox 61 OS: Windows Steps or JS usage snippet reproducing the issue:

I'm trying to get the monaco-editor (v0.13.1) to check function signatures in JavaScript. By using the code below, I at least get IntelliSense to show the intended signature, but no error is shown:

    // validation settings
    monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({
    	noSemanticValidation: false,
    	noSyntaxValidation: false,
    });
    
    monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({
    	noSemanticValidation: false,
    	noSyntaxValidation: false,
    });
    
    // compiler options
    monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
    	target: monaco.languages.typescript.ScriptTarget.ES6,
    	allowNonTsExtensions: true,
        allowJS: true,
    });
    
    var jsCode = `
    /**
     * @param {string} a
     * @param {number} [b]
     */
    function foo(a, b) {
    
    }
    
    foo(1); // here should be an error!
    `
    
    monaco.editor.create(document.getElementById("container"), {
    	value: jsCode,
    	language: "javascript"
    });

What am I missing?

AlCalzone avatar Aug 05 '18 13:08 AlCalzone

Hi, You can use the below snippet. That worked for me.

var indexJSValue = `// index.js
console.log(value);
export function hello() {
}`;
var lang = 'typescript';
var model = new monaco.editor.createModel(indexJSValue, lang, monaco.Uri.from({path: "index.js"}))
monaco.editor.create(document.getElementById("container"), {
	model: model,
	language: "javascript"
});

var tsOp = monaco.languages.typescript.typescriptDefaults['getCompilerOptions']();
tsOp.allowJs = true;
tsOp.checkJs = true;
tsOp.strict = true;
monaco.languages.typescript.typescriptDefaults.setCompilerOptions(tsOp);

yln99517 avatar Aug 07 '18 11:08 yln99517

That helps, thank you! Apparently the model needs to have an URI ending with ".js" for the JS mode to be active.

Meanwhile I found #990

AlCalzone avatar Aug 07 '18 12:08 AlCalzone