Search code examples
javascriptvisual-studio-codetypecheckingduck-typing

Is there a way to make JS editor in VS Code mark identifiers with unknown/any type (which are only presumed to exist)?


When pasting/moving code around in JS, it'd be useful to be able to find functions/variables which have no known type. Their type is often unknown because of code that might be missing (or type definitions that you might want to add in TS/JSDoc).

Currently, I have to hover the mouse on every identifier to check whether or not they have a known type. My current theme is Monokai, and it doesn't make this distinction.

I wonder if there is some sort of setting/theme/extension that lets me easily see when an identifier is only presumed to exist, without guarantee (for example, an external global variable or property). I mean something that would make these identifiers have a different color, or maybe an info/warning underline marker. I think NetBeans might have a feature somewhat like this, because I remember global vars having a different color.


Solution

  • when an identifier is only presumed to exist, without guarantee - for example, an external global variable or property

    That sounds more like you are looking to find undeclared variables. That's pretty simple due to the good scoping rules in modern JavaScript, and most linters will complain about (and highlight) these by default. For example, take the no-undef rule in eslint.

    Find functions/variables which have no known type. Their type is often unknown because of code that might be missing (or type definitions that you might want to add in TS/JSDoc).

    This sounds more like a typescript question. But yes, there are tools that will complain about variables whose type was not declared / could not be inferred as well. See Is there any way to get warnings (not errors) for noImplicitAny noncompliance? for details.