Implicit function scope
See original GitHub issueThis is slightly related to #953 - the difference is I would like some code to not be visible at all. I am aware of addExtraLib and use that to inject some custom global functions and classes for IntelliSense purposes.
The use case I have is that I want a user to write a function’s body only (the function declaration is not part of the model). This works fine until, for instance, the user writes something like:
let varName = 0;
If varName is already declared somewhere with the addExtraLib call, this will give a “Cannot redeclare block-scoped variable ‘varName’.(2451)” error. (This won’t be the case during the actual execution, but there’s no way to tell monaco that!)
It’s also impossible to have a return statement, as that will cause an “A ‘return’ statement can only be used within a function body. (1108)” error.
I would like the code to be validated as though it is in its own function scope, as is the case within a function, so this error is no longer reported.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)

Top Related StackOverflow Question
You can work around that pretty easily by only including necessary types in your extra lib instead of the actual code you wrap the user’s code in. Consider the following function with parameters and a predefined global object
foowith one methodbar()that returns a number:Then you want your users to just write
into the editor and get completion for everything, as far as I understand. The
returnstatements normally gives you an error which you can suppress by the method mentioned earlier. So you only need to supplya,bandfooin your extra lib and can use a.d.tsfile for that:This is how we use the editor for this exact use case. Provide the necessary types by a
.d.tsfile in the extra lib and suppress the error for thereturnstatement. Since you only provide the necessary types there is no chance of name clashes and you probably don’t want the user to be able to overwrite the global objects anyway. This is why you declare the global object asconstso the user gets an error if they overwrite/redeclare it, and usevarfor parameters if you want the user to be able to overwrite those.Does this help you?
Full playground example adopted from https://microsoft.github.io/monaco-editor/playground.html#extending-language-services-configure-javascript-defaults:
You’re right that it sounds a bit strange.
To explain my use case a bit more, users of our application can create all kinds of identifiers through a web UI that translate in code to classes/constants/functions.
A user had one of those classes named
resultand would later add a function with alet result = 0;statement in its body (as one does). Of course, this is perfectly valid if the code was in an actual function, you can just redeclare variables if you want. But as the extraLib and the user code resides in the same block-scope, this would hint to the user that they couldn’t useresult. The code executes fine when tested, so there was a discrepancy.You could say that they shouldn’t use the name ‘result’ as a class, but it’s how they happened to compose their model. 🤷♂️
Anyway, thanks again for the tips!