์์ js์ฝ๋์ ๋ค์๊ณผ ๊ฐ์ ์ฃผ์์ ์ถ๊ฐํ๋ฉด type ์ฒดํฌ๊ฐ ๊ฐ๋ฅํ๋ค.
1
// @ts-check
์ด๋ JSDoc์ ์ด์ฉํ๋ฉด ํ์ ์ ๋ํ ํํธ๋ฅผ ์ ๊ณตํ ์ ์๋ค
JSDoc์ด๋?
์๋ฐ์คํฌ๋ฆฝํธ API ๋ฌธ์ ์์ฑ๊ธฐ๋ก, API๋ฅผ ์ค๋ช ํ๋ HTML์ ์์ฑํ ์ ์๋ค. ์ผ๋ฐ ์ฃผ์๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ฌด์๋.
1 ๋ณ์ํ์
1
2
3
4
/**
* @type {string}
*/
let str;
2 ํจ์ํ์
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// TypeScript syntax๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
/**
* @type {(a:number, b: number) => number}
*/
const add = (a, b) => a + b;
// Closure syntax๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
/**
* @type {function(number, number): number}
*/
const multiply = (a, b) => a * b;
/**
* @param {string} p1 - A string of param
* @param {string=} p2 - An optional param(Closure syntax)
* @param {string} [p3] - Another optional param (JSDoc syntax)
* @param {string} [p4="test"] - An optional param with a default value
* @return {string} This is the result
*/
function stringsStringStrings(p1, p2, p3, p4) {
//...
}
3 ํ์ ์ ์ - ๋ณต์กํ ํ์ ์ ์ ์ํ ๋ ์ฌ์ฉํ๋ค
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* ํ ์ผ
* @typedef {Object} Todo
* @property {number} id - ํ ์ผ id
* @property {string} content - ํ ์ผ ๋ด์ฉ
* @property {boolean} completed - ํ ์ผ ์๋ฃ ์ฌ๋ถ
*/
/**
* ํ ์ผ ๋ชฉ๋ก
* @typedef {Todo[]}
*/
const todos = [
{ id: 1, content: "HTML", completed: false },
{ id: 2, content: "CSS", completed: true },
];
- 4
@callback - @typedef์ ์ ์ฌํ์ง๋ง object ํ์ ๋์ ํน์ ํ function ํ์ ์ ์ง์ ํ๋ค.