forked from microsoft/TypeScript
- Notifications
You must be signed in to change notification settings - Fork 0
JSDoc support in JavaScript
Daniel Rosenwasser edited this page Aug 27, 2016
·
1 revision
The below code outlined which constructs are currently support or not supported when using JsDoc annotations to provide type information in JavaScript files.
Note any tags not listed explicitly below (such as @typedef, or @constructor)
are not yet supported.
// === Supported ===// You can use the "@type" tag and reference a type name (either primitive, // or defined in a TypeScript declaration)/** * @type {string} */varvar1;/** @type {Window} */varvar2;/** @type {PromiseLike<string>} */varvar3;// Likewise, for the return type of a function/** * @return {PromiseLike<string>} */functionfn1(){}/** * @returns {{a: string, b: number}} - May use '@returns' as well as '@return' */functionfn2(){}/** * The type specifier can specify a union type - e.g. a string or a boolean * @type {(string | boolean)} */varvar4;/** * Note that parens are options for union types * @type {string | boolean} */varvar5;// You can specify an array type (e.g. an array of numbers)/** @type {number[]} */varvar6;// An array of numbers (alternate syntax)/** @type {Array.<number>} */varvar7;/** @type {Array<number>} */varvar8;// An object specification may also be using within the braces// For example, an object with properties 'a' (string) and 'b' (number)/** @type {{a: string, b: number}} */varvar9;// Parameters may be delcared in a variety of syntactic forms/** * @param {string} p1 - A string param. * @param {string=} p2 - An optional param * @param {string} [p3] - Another optional param. * @param {string} [p4="test"] - An optional param with a default value * @return {string} This is the result */functionfn3(p1,p2,p3,p4){// TODO}// Generic types may also be used/** * @template T * @param {T} p1 - A generic parameter that flows through to the return type * @return {T} */functionfn4(p1){}/** @type {function(string, boolean): number} */varfn5;/** * @param {*} p1 - Param can be 'any' type * @param {?} p2 - Param is of unknown type (same as 'any') */functionfn6(p1,p2){}varsomeObj={/** * @param {string} param1 - Docs on property assignments work */x: function(param1){}};/** * As do docs on variable assignments * @return {Window} */letsomeFunc=function(){};/** * And class methods * @param {string} greeting The greeting to use */Foo.prototype.sayHi=(greeting)=>console.log("Hi!");/** * And arrow functions expressions * @param {number} x - A multiplier */letmyArrow=x=>x*x;/** * Which means it works for stateless function components in JSX too * @param {{a: string, b: number}} test - Some param */varsfc=(test)=><div>{test.a.charAt(0)}</div>;// === Below forms are not supported ===/** @type {Object.<string, number>} */varvar10;/** * @param {object} param1 - Listing properties on an object type does not work * @param {string} param1.name */functionfn7(param1){}functionFN8(){}/** * Refering to objects in the value space as types doesn't work * @type {FN8} */varvar11;/** @type {{a: string, b: number=}} */varvar12;// Optional members of object literals (optionality is ignored)/** @type {?number} */varvar13;// A 'nullable' number (treated as just 'number')/** @type {!number} */varvar14;// A 'non-nullable' number (treated as just 'number')/** * @param {...string} - A 'rest' arg (array) of strings. (treated as 'any') */functionfn9(p1){}// Inline JsDoc comments (treated as 'any')functionfn10(/** string */p1){}TypeScript Language Basics
- Basic Types
- Interfaces
- Classes
- Namespaces and Modules
- Functions
- Generics
- Compiler Options
- tsconfig.json
- Integrating with Build Tools
- Nightly Builds
TypeScript Language Advanced
- Mixins
- Declaration Merging
- Type Inference
- Type Compatibility
- JSX
- Writing Declaration Files
- Typings for NPM packages
News
TypeScript Contributors
- Contributing to TypeScript
- TypeScript Design Goals
- Coding Guidelines
- Spec conformance testing
- Useful Links for TypeScript Issue Management
- Writing Good Design Proposals
- Compiler Internals
Building Tools for TypeScript
- Architectural Overview
- Using the Compiler API
- Using the Language Service API
- Dev Mode in Visual Studio
- Debugging Language Service in VS Code
FAQs