์์ ํ์
โ ๋ถ๋ณ์ด๋ฉฐ, ๊ฐ์ฒด๊ฐ ์๋ ๊ฐ๋ค์ ์๋ฏธํ๋ค. (ํ์
์ถ๋ก ์ด ๋จ)
// 1. Boolean
const Boo: Boolean = false;
// 2. Number
const num: number = 123;
// 3. String
const str: string = '123';
// 4. Bigint
const big: bigint = 100n;
/* ์ด๋ ๊ฒ ์ง์ ์ง์ ํด ์ฃผ์ด๋ ๋๋ค. */
const strTata: 'tata' = 'tata';
๊ฐ์ฒด ํ์
// 1. Object
// ์ด๋ ๊ฒ ์ฌ์ฉํ๋ฉด any์ ๋ค๋ฅผ ๊ฒ ์๋ค.
const obj: object = { a: 'TATA' };
// ์ด๋ ๊ฒ ์ฌ์ฉํ๋ฉด ๋จ
const obj2: { str: string, num: number } = {
str: 'tata',
num: 7,
}
// 2. Enum
enum BT21 {
TATA, /* 0 */
CHIMMY = 7, /* 7 */
RJ /* 8 */
}
const bt21: BT21 = BT21.TATA /* ์ด๋ฐ ์์ผ๋ก ์ฌ์ฉํจ */
ํจ์ ํ์
// 1. Void
// ํจ์์์ ์ฌ์ฉํ๋ฉด => ๋ฆฌํด๋๋ ๊ฐ์ด ์๋ค๋ ์๋ฏธ
function a(): void {
console.log('TATA');
}
// 2. Never
// ์ ๋ ๋ฐ์ํ ์ ์๋ ํ์
function loop(): never {
while(true) {
console.log('TATA');
}
}
// 3. string์ด๋ number๋ก ๋ฐํ ํ์
์ ์ง์ ํด ์ฃผ์ด๋ ๋๋,
// ํ์
์ถ๋ก ์ด ๋๊ธฐ์ ๊ตณ์ด ์ ์ด์ฃผ์ง ์์๋ ๋๋ค.
// (๋งค๊ฐ๋ณ์ ํ์
์ ์ง์ ํด ์ฃผ๊ธฐ)
function b(num: number, str: string): number {
return num + Number(str);
}
b(777, '777')
// 4. Object
function c(obj: { num: number, str: string }) {
return obj.num + obj.str;
}
c({ 7, 'tata' })
// 5. Function
// ์ฌ์ฉํ์ง ์๋ ๊ฒ์ด ์ข๋ค.
function d(num: Function): Function {
return num
}
๋ฐฐ์ด ํ์
// 1. Array
const numList: number[] = [1, 2, 3];
const strList: string[] = ['1', '2', '3'];
const booList: boolean[] = [true, false, true];
// โ ์ด๋ฐ ๋ฐฉ์๋ ์์
// const numList2: Array<number> = [1, 2, 3];
// const strList2: Array<string> = ['1', '2', '3'];
// 2. Tuple
const tuple: [number, string] = [1, "1"]
const tuple2: [number, ...string[]] = [1, '1', '2']
๋ฆฌํฐ๋ด ํ์
// 1. let
// ๋ณ์์ ๊ฐ์ ๋ณ๊ฒฝํ ์ ์๋ค.
let letStr = 'tata' // string
// 2. const
// ๋ณ์์ ๊ฐ์ ๋ณ๊ฒฝํ ์ ์๋ค.
const conStr = 'tata' // 'tata'
undefined / null
// 1. undefined
const a: number | undefined = undefined;
// 2. null
const b: number | null = null;
any / unknown
// 1. any
const a: any = 4;
const b: any = 'string';
// 2. unknown
// => any์ฒ๋ผ ๋ชจ๋ ๊ฐ์ ํ์ฉํ์ง๋ง ์๋์ ์ผ๋ก ์๊ฒฉํ๋ฉฐ,
// ์ง์ ๋ช
์ํด ์ฃผ์ด์ผ ํ๋ค.
// (assertion ๋๋ ํ์
๊ฐ๋์ ํจ๊ป ์ฌ์ฉํจ)
let a: unknown = 7;
if (typeof a === 'string') {
a.trim();
}
// (a as string).trim();
์ธํฐํ์ด์ค
// - interface: ํ์
์ ์
// - implements: ๊ตฌํ
interface User {
nickname: string;
age?: number; // ?๊ฐ ์์ผ๋ฉด ํ์๋ก ๊ตฌํํ์ง ์์๋ ๋๋ค.
introduce(): void;
}
// ๋ค๋ฅธ interface์์ implements ๊ฐ๋ฅ
// class์์ extends๋ฅผ ์ฌ์ฉํ ์ํ์์๋ implements ๊ฐ๋ฅ
class Play implements User {
nickname: string;
constructor(nickname: string) {
this.nickname = nickname;
}
introduce() {
console.log(`์ ๋๋ค์์ ${this.nickname} ์
๋๋ค.`);
}
}
const a = new Play("TATA");
a.introduce(); // "์ ๋๋ค์์ TATA ์
๋๋ค."
type
type str = string;
type num = number;
type arr = string[];
type func = () => void;
// ์ฌ์ฌ์ฉ
type User = {
nickname: str;
age: num;
playInfo: func;
}
์ ๋ค๋ฆญ
function User<T>(name: T): T {
return name;
}
console.log(User<string>('TATA')) // "TATA"
console.log(User<number>(142501)) // 142501
console.log(User<string[]>(['TATA', 'CHIMMY'])) // ["TATA", "CHIMMY"]
-------
// type๊ณผ interface์์ ์ ๋ค๋ฆญ ์ฌ์ฉ๋ฒ
type UserTypeFn = <T>(name: T) => T
interface UserInterFn {
<T>(name: T): T;
}
-------
// class์์ ์ ๋ค๋ฆญ ์ฌ์ฉ๋ฒ
class User<T> {
name: T
constructor(name: T) {
this.name = name;
}
}
// const a = new User<string>('TATA');
const a = new User('TATA');
console.log(a.name); // "TATA"
-------
// ์ ๋ค๋ฆญ extends ์ฌ์ฉ๋ฒ
function User<T extends string | number>(name: T): T {
return name;
}
// string๊ณผ numberํ์
๋ง ์ฌ์ฉ ๊ฐ๋ฅ
console.log(User('TATA')); // "TATA"
console.log(User('12546')); // "12546"
typeof
๋ณ์, ํจ์, ํด๋์ค ๋๋ ๊ฐ์ฒด์ ํ์ ์ ์ถ์ถํ๋ค.
let num = 10;
let numType: typeof num; // num ๋ณ์์ ํ์
์ ์ถ์ถํ์ฌ ํ ๋น
typeof, keyof
typeof
: ํ์
์ถ์ถ
keyof
: ๊ฐ์ฒด์ ํค๋ง ์ถ์ถ
export const Color = {
Red: "red",
Green: "green",
Blue: "blue",
} as const;
type ColorType = typeof Color;
/*
ColorType์ ์๋์ฒ๋ผ ๋จ:
{
Red: "red";
Green: "green";
Blue: "blue";
}
*/
type ColorKeys = keyof typeof Color;
// type ColorKeys = "Red" | "Green" | "Blue";
type ColorValues = typeof Color[keyof typeof Color];
// type ColorValues = "red" | "green" | "blue";
๋ฐฐ์ด์์ ์ ๋์จ ํ์
๋ง๋ค๊ธฐ [number]
const rgb = ['red', 'green', 'blue'] as const;
type RGB = (typeof rgb)[number];
// typeof rgb๋ readonly ["red", "green", "blue"]๋ผ๋ ํํ ํ์
.
// typeof rgb[number] โ "red" | "green" | "blue" (๋ฐฐ์ด์ ์์๋ค์ ์ ๋์จ ํ์
์ผ๋ก ๋ณํ).
// "red" | "green" | "blue"
type RedAndBlue = Exclude<RGB, 'blue'>;
// "red" | "green"
๐ฅฆtypescript์ class ์ฌ์ฉ๋ฒ ๋ณด๋ฌ๊ฐ๊ธฐ
๐ TypeScript - Playground