Adding JavaScript type hints for VSCode/Monaco Intellisence
up vote
3
down vote
favorite
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
But it doesn't know what gl
is
Nor does it know what ctx
is
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
and it 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.
javascript intellisense visual-studio-code monaco-editor
add a comment |
up vote
3
down vote
favorite
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
But it doesn't know what gl
is
Nor does it know what ctx
is
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
and it 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.
javascript intellisense visual-studio-code monaco-editor
1
JSDoc works in Monaco since version 0.90. See stackoverflow.com/a/45181853/2102158.
– Jon N
Jul 19 '17 at 5:46
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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
But it doesn't know what gl
is
Nor does it know what ctx
is
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
and it 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.
javascript intellisense visual-studio-code monaco-editor
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
But it doesn't know what gl
is
Nor does it know what ctx
is
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
and it 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.
javascript intellisense visual-studio-code monaco-editor
javascript intellisense visual-studio-code monaco-editor
edited Sep 26 '16 at 3:51
asked Sep 26 '16 at 3:46
gman
45.3k17109192
45.3k17109192
1
JSDoc works in Monaco since version 0.90. See stackoverflow.com/a/45181853/2102158.
– Jon N
Jul 19 '17 at 5:46
add a comment |
1
JSDoc works in Monaco since version 0.90. See stackoverflow.com/a/45181853/2102158.
– Jon N
Jul 19 '17 at 5:46
1
1
JSDoc works in Monaco since version 0.90. See stackoverflow.com/a/45181853/2102158.
– Jon N
Jul 19 '17 at 5:46
JSDoc works in Monaco since version 0.90. See stackoverflow.com/a/45181853/2102158.
– Jon N
Jul 19 '17 at 5:46
add a comment |
3 Answers
3
active
oldest
votes
up vote
5
down vote
accepted
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");
This technique doesn't appear to work for imported types. e.g.,var Foo = require("foo"); /** @type {Foo} */ var foo;
In this example,Foo
is a type that VSCode recognizes and has full intellisense on. If instead you dovar Foo = require("foo"); var foo = new Foo();
everything works. Any pro-tips to make this work correctly with imported types?
– Micah Zoltu
Mar 3 '17 at 20:22
@MicahZoltu I had a similar issue annotating types from the Express library. I found how to make this work and posted an additional answer on this question. stackoverflow.com/a/48752318/1359579
– DannyMeister
Feb 12 at 17:39
add a comment |
up vote
1
down vote
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
add a comment |
up vote
0
down vote
If you are willing to use Typescript, you can do this:
var gl : WebGLRenderingContext;
Thanks! Unfortunately I'm using it on a site that's teaching stuff with JS not TS 😭
– gman
Jan 12 '17 at 4:06
@gman Well typescript is pretty similar to javascript. much of it is very similar to javascript, very easy to convert over
– kinger6621
Feb 25 at 22:29
1
@kinger6621 Nope, it's no easy: once that you are in TypeScript everything is "typed", and you must do follow with that.
– 0zkr PM
Sep 23 at 3:21
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
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");
This technique doesn't appear to work for imported types. e.g.,var Foo = require("foo"); /** @type {Foo} */ var foo;
In this example,Foo
is a type that VSCode recognizes and has full intellisense on. If instead you dovar Foo = require("foo"); var foo = new Foo();
everything works. Any pro-tips to make this work correctly with imported types?
– Micah Zoltu
Mar 3 '17 at 20:22
@MicahZoltu I had a similar issue annotating types from the Express library. I found how to make this work and posted an additional answer on this question. stackoverflow.com/a/48752318/1359579
– DannyMeister
Feb 12 at 17:39
add a comment |
up vote
5
down vote
accepted
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");
This technique doesn't appear to work for imported types. e.g.,var Foo = require("foo"); /** @type {Foo} */ var foo;
In this example,Foo
is a type that VSCode recognizes and has full intellisense on. If instead you dovar Foo = require("foo"); var foo = new Foo();
everything works. Any pro-tips to make this work correctly with imported types?
– Micah Zoltu
Mar 3 '17 at 20:22
@MicahZoltu I had a similar issue annotating types from the Express library. I found how to make this work and posted an additional answer on this question. stackoverflow.com/a/48752318/1359579
– DannyMeister
Feb 12 at 17:39
add a comment |
up vote
5
down vote
accepted
up vote
5
down vote
accepted
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");
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");
edited Jan 27 at 12:13
answered Sep 26 '16 at 3:58
gman
45.3k17109192
45.3k17109192
This technique doesn't appear to work for imported types. e.g.,var Foo = require("foo"); /** @type {Foo} */ var foo;
In this example,Foo
is a type that VSCode recognizes and has full intellisense on. If instead you dovar Foo = require("foo"); var foo = new Foo();
everything works. Any pro-tips to make this work correctly with imported types?
– Micah Zoltu
Mar 3 '17 at 20:22
@MicahZoltu I had a similar issue annotating types from the Express library. I found how to make this work and posted an additional answer on this question. stackoverflow.com/a/48752318/1359579
– DannyMeister
Feb 12 at 17:39
add a comment |
This technique doesn't appear to work for imported types. e.g.,var Foo = require("foo"); /** @type {Foo} */ var foo;
In this example,Foo
is a type that VSCode recognizes and has full intellisense on. If instead you dovar Foo = require("foo"); var foo = new Foo();
everything works. Any pro-tips to make this work correctly with imported types?
– Micah Zoltu
Mar 3 '17 at 20:22
@MicahZoltu I had a similar issue annotating types from the Express library. I found how to make this work and posted an additional answer on this question. stackoverflow.com/a/48752318/1359579
– DannyMeister
Feb 12 at 17:39
This technique doesn't appear to work for imported types. e.g.,
var Foo = require("foo"); /** @type {Foo} */ var foo;
In this example, Foo
is a type that VSCode recognizes and has full intellisense on. If instead you do var Foo = require("foo"); var foo = new Foo();
everything works. Any pro-tips to make this work correctly with imported types?– Micah Zoltu
Mar 3 '17 at 20:22
This technique doesn't appear to work for imported types. e.g.,
var Foo = require("foo"); /** @type {Foo} */ var foo;
In this example, Foo
is a type that VSCode recognizes and has full intellisense on. If instead you do var Foo = require("foo"); var foo = new Foo();
everything works. Any pro-tips to make this work correctly with imported types?– Micah Zoltu
Mar 3 '17 at 20:22
@MicahZoltu I had a similar issue annotating types from the Express library. I found how to make this work and posted an additional answer on this question. stackoverflow.com/a/48752318/1359579
– DannyMeister
Feb 12 at 17:39
@MicahZoltu I had a similar issue annotating types from the Express library. I found how to make this work and posted an additional answer on this question. stackoverflow.com/a/48752318/1359579
– DannyMeister
Feb 12 at 17:39
add a comment |
up vote
1
down vote
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
add a comment |
up vote
1
down vote
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
add a comment |
up vote
1
down vote
up vote
1
down vote
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
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
answered Feb 12 at 17:37
DannyMeister
7141715
7141715
add a comment |
add a comment |
up vote
0
down vote
If you are willing to use Typescript, you can do this:
var gl : WebGLRenderingContext;
Thanks! Unfortunately I'm using it on a site that's teaching stuff with JS not TS 😭
– gman
Jan 12 '17 at 4:06
@gman Well typescript is pretty similar to javascript. much of it is very similar to javascript, very easy to convert over
– kinger6621
Feb 25 at 22:29
1
@kinger6621 Nope, it's no easy: once that you are in TypeScript everything is "typed", and you must do follow with that.
– 0zkr PM
Sep 23 at 3:21
add a comment |
up vote
0
down vote
If you are willing to use Typescript, you can do this:
var gl : WebGLRenderingContext;
Thanks! Unfortunately I'm using it on a site that's teaching stuff with JS not TS 😭
– gman
Jan 12 '17 at 4:06
@gman Well typescript is pretty similar to javascript. much of it is very similar to javascript, very easy to convert over
– kinger6621
Feb 25 at 22:29
1
@kinger6621 Nope, it's no easy: once that you are in TypeScript everything is "typed", and you must do follow with that.
– 0zkr PM
Sep 23 at 3:21
add a comment |
up vote
0
down vote
up vote
0
down vote
If you are willing to use Typescript, you can do this:
var gl : WebGLRenderingContext;
If you are willing to use Typescript, you can do this:
var gl : WebGLRenderingContext;
answered Jan 11 '17 at 4:25
Doug
4,73683965
4,73683965
Thanks! Unfortunately I'm using it on a site that's teaching stuff with JS not TS 😭
– gman
Jan 12 '17 at 4:06
@gman Well typescript is pretty similar to javascript. much of it is very similar to javascript, very easy to convert over
– kinger6621
Feb 25 at 22:29
1
@kinger6621 Nope, it's no easy: once that you are in TypeScript everything is "typed", and you must do follow with that.
– 0zkr PM
Sep 23 at 3:21
add a comment |
Thanks! Unfortunately I'm using it on a site that's teaching stuff with JS not TS 😭
– gman
Jan 12 '17 at 4:06
@gman Well typescript is pretty similar to javascript. much of it is very similar to javascript, very easy to convert over
– kinger6621
Feb 25 at 22:29
1
@kinger6621 Nope, it's no easy: once that you are in TypeScript everything is "typed", and you must do follow with that.
– 0zkr PM
Sep 23 at 3:21
Thanks! Unfortunately I'm using it on a site that's teaching stuff with JS not TS 😭
– gman
Jan 12 '17 at 4:06
Thanks! Unfortunately I'm using it on a site that's teaching stuff with JS not TS 😭
– gman
Jan 12 '17 at 4:06
@gman Well typescript is pretty similar to javascript. much of it is very similar to javascript, very easy to convert over
– kinger6621
Feb 25 at 22:29
@gman Well typescript is pretty similar to javascript. much of it is very similar to javascript, very easy to convert over
– kinger6621
Feb 25 at 22:29
1
1
@kinger6621 Nope, it's no easy: once that you are in TypeScript everything is "typed", and you must do follow with that.
– 0zkr PM
Sep 23 at 3:21
@kinger6621 Nope, it's no easy: once that you are in TypeScript everything is "typed", and you must do follow with that.
– 0zkr PM
Sep 23 at 3:21
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39694407%2fadding-javascript-type-hints-for-vscode-monaco-intellisence%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
JSDoc works in Monaco since version 0.90. See stackoverflow.com/a/45181853/2102158.
– Jon N
Jul 19 '17 at 5:46