monaco-editor
monaco-editor copied to clipboard
How to enforce function signatures in JavaScript?
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?
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);
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