ts-check
TypeScript 2.3부터 유형주석이 있는 기존 JavaScript 코드를 분석 할 수 있도록 지원합니다.
vs code
자바 스크립트에서 TypeScript 분석을 켜려면 vs code 에디터상에서 라는 텍스트가 있는 주석을 파일의 시작 부분에 추가하기만하면 됩니다.
그런 다음 파일의 아무 곳에나 TypeScript의 JSDoc 유형의 주석을 추가하면 됩니다.
jsdoc
// @ts-check
/**
* @type {string}
*/
var var1;
/**
* The type specifier can specify a union type - e.g. a string or a boolean
* @type {(string | boolean)}
*/
var var4;
// You can specify an array type (e.g. an array of numbers)
/** @type {number[]} */
var var6;
// @ts-check
// Likewise, for the return type of a function
/**
* @return {PromiseLike}
*/
function fn1(){}
/**
* @returns {{a: string, b: number}} - May use '@returns' as well as '@return'
*/
function fn2(){}
// @ts-check
/**
* @param {number} a
* @param {number} b
* @return {number}
*/
function example(a, b) {
return a + b;
}
TypeScript가 내장된 vs code 에서는 이러한 주석을 자동으로 감지하여 사용자가 기대하는대로 수행합니다.
객체의 구조
주석 기능 외에도 임의의 객체의 구조를 기술 할 수 있습니다.
/**
* @typedef {Object} Issue
* @property {string} url
* @property {string} repository_url
* @property {id} number
* @property {string} title
* @property {string} state
* @property {bool} open
*/
const url = 'https://api.github.com/repos/microsoft/typescript/issues';
(async () => {
let response = await got(url, {json: true});
/** @type {Issue[]} */
let issues = response.body;
for (let issue of issues)
console.log(issue.title);
})();