ํ์ ์คํฌ๋ฆฝํธ ๊ธฐ๋ณธํ
์ข ๋ฅ | ํ์ |
๋ฌธ์์ด | string |
์ซ์ํ | number |
๋ถ๋ฆฐํ | boolean |
undefined | undefined |
null | null |
๋ฐฐ์ด๊ณผ ํํ
const cart: string[] = [];
cart.push('c001');
cart.push('c002');
// ๋ฐฐ์ด์ ๋ฐฐ์ด
const carts: string[][] = [
['c001', 'c002'],
['c003'],
];
// ํํ
let mySize: [number, number, string] = [175, 30, 'L'];
๊ฐ์ฒด ํ์
let product: {
id: string;
name: string;
price: number;
membersOnly?: boolean; // ํ์๊ฐ ์๋ ํ๋กํผํฐ
sizes: string[];
} = {
id: 'c001',
name: '์ฝ๋์ ๋ธ๋ ํ๋',
price: 129000,
sizes: ['M', 'L', 'XL'],
};
let stock: { [id: string]: number } = {
c001: 3,
c002: 0,
c003: 2,
};
any ํ์
ํ์ ์ถ๋ก ์ด ๋์ง ์๋ ๊ฒฝ์ฐ์ ํ์ ์ any๊ฐ ๋๋๋ฐ ๋๋๋ก ์ฌ์ฉํ์ง ๋ง์์ผ ํ๋ค.
const parsedProduct = JSON.parse('{ "name": "์ฝ๋์ ํ ํธ๋ฐฑ", "price": 12000 }')
parsedProduct์ ํ์ ์ any๊ฐ ๋๋ค.
์์ ํด๊ฒฐ๋ฐฉ์1
const parsedProduct = JSON.parse('{ "name": "์ฝ๋์ ํ ํธ๋ฐฑ", "price": 12000 }') as { name: string; price: number };
์์ ํด๊ฒฐ๋ฐฉ์2
const parsedProduct: { name: string; price: number } = JSON.parse('{ "name": "์ฝ๋์ ํ ํธ๋ฐฑ", "price": 12000 }');
ํจ์ ํ์ ์ง์
function addToCart(id: string, quanity: number): boolean {
if (์ด๋ค ์กฐ๊ฑด) {
return false;
}
return true;
}
์์ ๋ฆฌํด ํ์ ์ ์ถ๋ก ์ด ๊ฐ๋ฅํ ๋ ์๋ตํด๋ ๋๋ค.
ํจ์๋ฅผ ๊ฐ์ผ๋ก ์ฌ์ฉํ๋ ๊ฒฝ์ฐ
(id:string, quanity: number) => boolean
(...ids: string[]) => void;
'TIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeafyLogํ๋ก์ ํธ] Typescript ๋ก ์ธํ ๋ฌธ์ ๋ค.. (0) | 2023.08.26 |
---|---|
[Typescript] enum ๋ฌธ๋ฒ ์ ๋ฆฌ (0) | 2023.08.23 |
Props์ ๋ฐ๋ผ components์ style ์ ์ฉํ๊ธฐ (0) | 2023.08.21 |
[next.js] ํ์ผ ์์คํ ๊ธฐ๋ฐ ๋ผ์ฐํ (0) | 2023.08.08 |
[์ค๋์ ์ฝ์ง์ผ๊ธฐ] props๋ฅผ state๋ก ์ฐ๋ฉด ... (0) | 2023.08.02 |