Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding JavaScript type hints for VSCode/Monaco Intellisence

Is there a way to hint to VSCode/Monaco's intellisense the types of variables.

I have some code like this

var loc = window.location;
var gl = context1.getContext("webgl");
var ctx = context2.getContext("2d");

I see that VSCode knows that loc is a URL

vscode knows loc

But it doesn't know what gl is

vscode does not know gl

Nor does it know what ctx is

vscode does not know ctx

Which makes sense, having a function return different types based on its input is a somewhat unusual case.

But it does have type data for WebGLRenderingContext

vscode knows webglrenderingcontext

and it knows CanvasRenderingContext2D

vscode knows canvasrenderingcontext2d

Is there a way to for me to tell vscode/monaco that gl is an instance of WebGLRenderingContext and that ctx is an instance of CanvasRenderingContext2D without having to switch to typescript? Maybe by adding some kind of comment?

I need the solution to work in monaco (which at least in my tests shows all the same completions) because this is for a WebGL tutorial site, not actually for VSCode but I'm hoping the solution is the same.

like image 876
gman Avatar asked Sep 26 '16 03:09

gman


People also ask

Does JavaScript support type hints?

Typed JavaScript is a system of tooling and conventions that exposes type hints and type errors before a program is run. JavaScript lacks a mandatory compile step, so it doesn't make much sense to talk about static typing. But Typed JavaScript offers the same main advantage as a compiled, statically typed language.

What is JavaScript IntelliSense?

IntelliSense# Visual Studio Code's JavaScript IntelliSense provides intelligent code completion, parameter info, references search, and many other advanced language features.

How do I use IntelliSense code in Visual Studio?

You can trigger IntelliSense in any editor window by typing Ctrl+Space or by typing a trigger character (such as the dot character (.) in JavaScript). Tip: The suggestions widget supports CamelCase filtering, meaning you can type the letters which are upper cased in a method name to limit the suggestions.


2 Answers

Update: As of 0.9.0 of Monaco these type annotations now work


Is see that JSDoc style type annotations work in VSCode though they don't appear to work in Monaco.

var loc = window.location;

/** @type {WebGLRenderingContext} */
var gl = context1.getContext("webgl");    

/** @type {CanvasRenderingContext2D} */
var ctx = context2.getContext("2d"); 

enter image description here

enter image description here

like image 191
gman Avatar answered Oct 18 '22 17:10

gman


As Micah pointed out in a comment on the accepted answer, there can still be issues with external modules. Simply doing a require of the module will already enable jsdoc type annotations to work if the library defines a global object from which you can reference the types. Otherwise, you can mimic this by importing everything and mapping it to your own name.

import * as Foo from 'foo'

/** @type {Foo.Foo} */
var foo;

https://github.com/Microsoft/TypeScript/issues/8237#issuecomment-213047062

like image 6
DannyMeister Avatar answered Oct 18 '22 17:10

DannyMeister