๐Ÿ’ป์›น(Web)/React

[TypeScript]TypeScript์˜ ๊ฐœ์š” ๋ฐ ๊ธฐ์ดˆ

stonesy 2024. 9. 3. 11:15
728x90

๐Ÿ“ŒTypeScript ๊ฐœ์š” ๋ฐ ๊ธฐ์ดˆ

โœจTypeScript ๊ฐœ์š”

TypeScript๋ž€ ์–ด๋–ค ์–ธ์–ด์ธ๊ฐ€?

TypeScript๋ž€ JavaScript๋ฅผ ๋” ์•ˆ์ •์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก Type์ด๋ผ๋Š” ์•ˆ์ „์žฅ์น˜๋ฅผ ์ถ”๊ฐ€ํ•œ ํ™•์žฅํŒ JavaScript๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.โ€ป์ฐธ๊ณ : tsconfig.js → TypeScript์˜ ์ปดํŒŒ์ผ๋Ÿฌ ์˜ต์…˜ ํŒŒ์ผ์ด๋‹ค.

โ€ป์ฐธ๊ณ : tsconfig.js → TypeScript์˜ ์ปดํŒŒ์ผ๋Ÿฌ ์˜ต์…˜ ํŒŒ์ผ์ด๋‹ค.

 

 

โœจTypeScript์˜ ์›์‹œํƒ€์ž…: number, string, boolean, null, undefined

TypeScript์˜ ์›์‹œํƒ€์ž…์ธ Type number, string, boolean, null, undefined์— ๋Œ€ํ•ด TypeScript๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

// type ์ฃผ์„ ๋˜๋Š” type annotation

// ๊ธฐ๋ณธ์ ์ธ 5๊ฐœ์˜ ์›์‹œ type: number, string, boolean, null, undefined
// number 
let num1: number = 123;
let num2: number = -123;
let num3: number = 0.123;
let num4: number = -0.123;
let num5: number = Infinity;
let num6: number = -Infinity;
let numb7: number = NaN;

// num1.toUpperCase(); error
num1.toFixed();

// string
let str1: string = "hello";
let str2: string = 'hello';
let str3: string =  `hello`;
let str4: string = `hello ${num1}`;

str1.toUpperCase();

// boolean
let bool1: boolean = true;
let bool2: boolean = false;

// null
let null1: null = null;

// undefined
let unde1: undefined = undefined;

// ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…
// ๋ฆฌํ„ฐ๋Ÿด -> ๊ฐ’์ด๋ผ๋Š” ๋œป
let numA: 10 = 10; // ํƒ€์ž…์œผ๋กœ ์ง€์ •ํ•œ ๊ฐ’ ์™ธ์—๋Š” ๋‹ค๋ฅธ ๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜ ์—†๋‹ค.
let strA: "hello" = "hello";
// let boolA: true = false; error

 

โœจTypeScript์˜ ๋ฐฐ์—ด, ํŠœํ”Œ

๋ฐฐ์—ด๊ณผ ํŠœํ”Œ์— ๋Œ€ํ•ด์„œ TypeScript๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ด๋•Œ, ํŠœํ”Œ์€ JavaScript์—์„œ๋Š” ์ง€์›ํ•˜์ง€ ์•Š๊ณ , TypeScript์—์„œ๋งŒ ์ง€์›ํ•˜๋Š” type์œผ๋กœ ๊ธธ์ด์™€ ํƒ€์ž…์ด ๊ณ ์ •๋œ ๋ฐฐ์—ด์„ ์˜๋ฏธํ•œ๋‹ค.

// ๋ฐฐ์—ด
let numArr:number[] = [1,2,3];
let strArr:string[] = ["hello", "ts", "winterlood"];
let boolArr:boolean[] = [true,false,true];
let boolArr2: Array<boolean> = [true, false, true];

// ๋ฐฐ์—ด์— ๋“ค์–ด๊ฐ€๋Š” ๊ฐ’์ด ๋‹ค์–‘ํ•œ ๊ฒฝ์šฐ
let multiArr: (number | string)[] = [1, "hello"];

// ๋‹ค์ฐจ์› ๋ฐฐ์—ด์˜ ํƒ€์ž…์„ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•
let doubleArr: number[][] = [
    [1,2,3],
    [4,5]
];

// ํŠœํ”Œ -> JavaScript์—๋Š” ์—†๊ณ  TypeScript์—์„œ๋งŒ ํŠน๋ณ„ํžˆ ์ œ๊ณต๋˜๋Š” ํƒ€์ž…
// ํŠœํ”Œ : ๊ธธ์ด์™€ ํƒ€์ž…์ด ๊ณ ์ •๋œ ๋ฐฐ์—ด 
let tup1:[number, number] = [1,2];
// tup1 = [1,2,3]; ๊ธธ์ด๋ฅผ ๋„˜์–ด์„œ๋Š” ๋ฐฐ์—ด์„ ์ €์žฅํ•  ์ˆ˜ ์—†๋‹ค.
// tup1 = ["1","2","3"]; Type์ด ๋‹ค๋ฅธ ๋ฐฐ์—ด์„ ์ €์žฅํ•  ์ˆ˜ ์—†๋‹ค.

let tup2: [number, string, boolean] = [1,"2",true];
const users: [string, number][] = [
    ["์ด์ •ํ™˜",1],
    ["์ด์•„๋ฌด๊ฐœ",2],
    ["๊น€์•„๋ฌด๊ฐœ",3],
    ["์ด์ •ํ™˜",4],
    ["์ด์ •ํ™˜",5],
]

 

 

โœจTypeScript ๊ฐ์ฒด

๊ตฌ์กฐ์  ํƒ€์ž… ์‹œ์Šคํ…œ = Property ๊ธฐ๋ฐ˜ ํƒ€์ž… ์‹œ์Šคํ…œ(Property-based type system) ↔ C์–ธ์–ด, Java๋Š” ๋ช…๋ชฉ์  ํƒ€์ž… ์‹œ์Šคํ…œ์ด๋‹ค.

์ฆ‰, TypeScript๋Š” ๋Œ€๋ถ€๋ถ„์˜ ์–ธ์–ด๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ชฉ์  ํƒ€์ž… ์‹œ์Šคํ…œ์ด ์•„๋‹Œ ๊ตฌ์กฐ์  ํƒ€์ž… ์‹œ์Šคํ…œ์„ ์‚ฌ์šฉํ•œ๋‹ค.

// object
// ๊ฒฐ๋ก ์ ์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
let user: {
    id?: number, // ์žˆ์–ด๋„ ๋˜๊ณ , ์—†์–ด๋„ ๋˜๋Š”๋ฐ ์žˆ๋‹ค๋ฉด type์ด number์—ฌ์•ผ ํ•œ๋‹ค.
    name: string
} = {
    id:1,
    name: "์ด์ •ํ™˜",
};

// ๊ฐ์ฒด์˜ type์„ object๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์  ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ ์ ‘๊ทผํ•˜๋ ค๊ณ  ํ•  ๋•Œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
// console.log(user.name); error

// let dog:{
//     name: string,
//     color: string,
// } = {
//     name: "๋Œ๋Œ์ด",
//     color: "brown"
// }

user = {
    name: "ํ™๊ธธ๋™",
};

 

*Optional Property

?๋ฅผ ๋ถ™์ž„์œผ๋กœ์จ ์„ ํƒ์  Property๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

// object
// ๊ฒฐ๋ก ์ ์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
let user: {
    id?: number, // ์žˆ์–ด๋„ ๋˜๊ณ , ์—†์–ด๋„ ๋˜๋Š”๋ฐ ์žˆ๋‹ค๋ฉด type์ด number์—ฌ์•ผ ํ•œ๋‹ค.
    name: string
} = {
    id:1,
    name: "์ด์ •ํ™˜",
};

// ๊ฐ์ฒด์˜ type์„ object๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์  ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ ์ ‘๊ทผํ•˜๋ ค๊ณ  ํ•  ๋•Œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
// console.log(user.name); error

// let dog:{
//     name: string,
//     color: string,
// } = {
//     name: "๋Œ๋Œ์ด",
//     color: "brown"
// }

user = {
    name: "ํ™๊ธธ๋™",
};

 

 

โœจํƒ€์ž… ๋ณ„์นญ๊ณผ ์ธ๋ฑ์Šค ์‹œ๊ทธ๋‹ˆ์ฒ˜

โœ”ํƒ€์ž… ๋ณ„์นญ์ด๋ž€?(Type Alias)

  • ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋“ฏ ํƒ€์ž…์„ ๋ณ„๋„๋กœ ์ •์˜ํ•˜๋Š” ๊ฒƒ
  • ๋™์ผํ•œ ์Šค์ฝ”ํ”„์— ๋™์ผํ•œ ์ด๋ฆ„์˜ ํƒ€์ž… ๋ณ„์นญ ์„ ์–ธ ๋ถˆ๊ฐ€

*๊ธฐ๋ณธ ํƒ€์ž… ๋ณ„์นญ

ํƒ€์ž… ๋ณ„์นญ์„ ํ†ตํ•ด ๊ฐ„๋‹จํ•œ ๊ธฐ๋ณธ ํƒ€์ž…์„ ์ƒˆ๋กœ์šด ์ด๋ฆ„์œผ๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

type UserId = string;
type Age = number;

let userId: UserId = "user123";
let userAge: Age = 25;

*๋ณต์žกํ•œ ํƒ€์ž… ๋ณ„์นญ

๊ฐ์ฒด์™€ ๊ฐ™์€ ๋ณต์žกํ•œ ๊ตฌ์กฐ์˜ ํƒ€์ž…๋„ ๋ณ„์นญ์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. User๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๊ฐ์ฒด ํƒ€์ž…์„ ์ •์˜ํ•˜์—ฌ, ๋™์ผํ•œ ๊ตฌ์กฐ์˜ ๊ฐ์ฒด๋ฅผ ์—ฌ๋Ÿฌ ๊ณณ์—์„œ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

type User = {
    id: UserId;
    name: string;
    age: Age;
    email: string;
};

let newUser: User = {
    id: "user123",
    name: "John Doe",
    age: 30,
    email: "[johndoe@example.com](<mailto:johndoe@example.com>)"
};

 

โœ”์ธ๋ฑ์Šค ์‹œ๊ทธ๋‹ˆ์ฒ˜

ํ”„๋กœํผํ‹ฐ์˜ ์ˆ˜๊ฐ€ ๋งŽ์„ ๋•Œ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ์— ๋Œ€ํ•ด type์„ ์ง€์ •ํ•˜๊ธฐ ๋ถˆํŽธํ•˜๋‹ค.

type CountryCodes = {
  [key: string]: string // key๊ฐ€ string ํƒ€์ž…์ด๊ณ  value๊ฐ€ string ํƒ€์ž…์ธ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ๋ฅผ ํฌํ•จํ•œ๋‹ค.
};

let countryCodes: CountryCodes = {
  Korea: "ko",
  UnitedState: "us",
  UnitedKingdom: "uk",
  // (... ์•ฝ 100๊ฐœ์˜ ๊ตญ๊ฐ€)
  Brazil : 'bz'
};

type CountryNumberCodes = {
  [key: string]: number; // ๊ตญ๊ฐ€ ์ฝ”๋“œ๋ฅผ ์ˆซ์ž๋กœ ๋ณด๊ด€ํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ ํ•˜๋‚˜ ๋” ํ•„์š”ํ•  ๋•Œ
  Korea: number; // ๋ฐ˜๋“œ์‹œ ํฌํ•จํ•ด์•ผ ํ•˜๋Š” ํ”„๋กœํผํ‹ฐ๊ฐ€ ์žˆ๋‹ค๋ฉด ๋”ฐ๋กœ ์ •์˜ ๊ฐ€๋Šฅ
  Italy: string; // ์˜ค๋ฅ˜! ํ•˜์ง€๋งŒ ์ถ”๊ฐ€์ ์ธ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ •์˜ํ•  ๋•Œ๋Š” ์ธ๋ฑ์Šค ์‹œ๊ทธ๋‹ˆ์ณ์˜ value ํƒ€์ž…๊ณผ ์ง์ ‘ ์ถ”๊ฐ€ํ•œ ํ”„๋กœํผํ‹ฐ์˜ value ํƒ€์ž…์ด ํ˜ธํ™˜๋˜๊ฑฐ๋‚˜ ์ผ์น˜ํ•ด์•ผ ํ•œ๋‹ค.
};

 

โœจEnum ํƒ€์ž…

Enum์€ ์ปดํŒŒ์ผ ๊ฒฐ๊ณผ ๊ฐ์ฒด๊ฐ€ ๋œ๋‹ค.

// enum ํƒ€์ž…
// ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๊ฐ’๋“ค์— ๊ฐ๊ฐ ์ด๋ฆ„์„ ๋ถ€์—ฌํ•ด ์—ด๊ฑฐํ•ด๋‘๊ณ  ์‚ฌ์šฉํ•˜๋Š” ํƒ€์ž…

// ์ˆซ์žํ˜• ์—ด๊ฑฐ
enum Role {
    ADMIN = 0,
    USER = 1,
    GUEST = 2,
}

const user1 = {
    name: "์ด์ •ํ™˜",
    role: Role.ADMIN, // ๊ด€๋ฆฌ์ž
} 

const user2 = {
    name: "ํ™๊ธธ๋™",
    role: Role.USER, // ํšŒ์›
}

const user3 = {
    name: "์•„๋ฌด๊ฐœ",
    role: Role.GUEST, // ๊ฒŒ์ŠคํŠธ
}

// enum์— ์ง์ ‘ ํ• ๋‹นํ•˜์ง€ ์•Š์œผ๋ฉด 0๋ถ€ํ„ฐ 1์”ฉ ๋Š˜์–ด๋‚˜๋Š” ๊ฐ’์œผ๋กœ ์ž๋™ ํ• ๋‹น๋œ๋‹ค.
enum Role2 {
    ADMIN, // 0 ํ• ๋‹น(์ž๋™)
    USER,  // 1 ํ• ๋‹น(์ž๋™)
    GUEST, // 2 ํ• ๋‹น(์ž๋™)
}


// ๋ฌธ์žํ˜• ์—ด๊ฑฐ
enum Language {
    korean = "ko",
    english = "en",
}
  
const user11 = {
    name: "์ด์ •ํ™˜",
    role: Role.ADMIN, // 0
    language: Language.korean,// "ko"
};

 

 

โœจAny์™€ Unkown ํƒ€์ž…

โœ”Any

Any๋Š” TypeScript๋ฅผ ์œ ๋ช…๋ฌด์‹คํ•˜๊ฒŒ ๋งŒ๋“ค๊ณ  ์œ„ํ—˜ํ•œ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์–ด ์ตœ๋Œ€ํ•œ ์‚ฌ์šฉ์„ ์ง€์–‘ํ•ด์•ผ ํ•œ๋‹ค.

  • ํƒ€์ž… ๊ฒ€์‚ฌ๋ฅผ ๋ฐ›์ง€ ์•Š๋Š”(ํƒ€์ž… ๊ณ„์ธต๋„๋ฅผ ๋”ฐ๋ฅด์ง€ ์•Š๋Š”) ์น˜ํŠธํ‚ค ํƒ€์ž…
  • ์•„๋ฌด ํƒ€์ž…์˜ ๊ฐ’์„ ๋‹ด๊ฑฐ๋‚˜, ์•„๋ฌด ํƒ€์ž…์˜ ๋ฉ”์„œ๋“œ๋„ ๋งˆ์Œ๋Œ€๋กœ ํ˜ธ์ถœ ๊ฐ€๋Šฅ
  • any ํƒ€์ž…์˜ ๊ฐ’์„ ์–ด๋–ค ํƒ€์ž…์œผ๋กœ ์ •์˜๋œ ๋ณ€์ˆ˜๋˜ ๋ฌธ์ œ ์—†์ด ๋‹ค ํ• ๋‹น ๊ฐ€๋Šฅ

โœ”Unknown

  • ์–ด๋–ค ํƒ€์ž…์˜ ๊ฐ’์ด๋“  ๋‹ค ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํ•˜์ง€๋งŒ unknown ํƒ€์ž…์˜ ๊ฐ’์€ ์–ด๋–ค ํƒ€์ž…์˜ ๋ณ€์ˆ˜์—๋„ ์ €์žฅํ•  ์ˆ˜ ์—†๋‹ค.
  • ๋˜ํ•œ unknown ํƒ€์ž…์˜ ๊ฐ’์€ ์–ด๋–ค ์—ฐ์‚ฐ์—๋„ ์ฐธ์—ฌํ•  ์ˆ˜ ์—†๊ณ , ์–ด๋–ค ๋ฉ”์„œ๋“œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.
  • unknown ํƒ€์ž…์˜ ๊ฐ’์„ ์—ฐ์‚ฐ์— ์ฐธ์—ฌํ•˜๊ฒŒ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ํƒ€์ž… ์ขํžˆ๊ธฐ๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค.
if (typeof unknownVar === "number") {
	// ์ด ์กฐ๊ฑด์ด ์ฐธ์ด๋œ๋‹ค๋ฉด unknownVar๋Š” number ํƒ€์ž…์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ์Œ
  unknownVar * 2;
}

 

โœจVoid์™€ Never ํƒ€์ž…

โœ”Void

  • ์•„๋ฌด๋Ÿฐ ๊ฐ’๋„ ์—†์Œ์„ ์˜๋ฏธํ•˜๋Š” ํƒ€์ž…
  • ๋ณดํ†ต ์•„๋ฌด๋Ÿฐ ๊ฐ’๋„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š” ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’ ํƒ€์ž…์„ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉ
function logMessage(message: string): void {
    console.log(message);
}

logMessage("This is a log message."); // ์ฝ˜์†”์— ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜์ง€๋งŒ ์•„๋ฌด ๊ฐ’๋„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š

 

 

โœ”Never

  • ๋ถˆ๊ฐ€๋Šฅ์„ ์˜๋ฏธํ•˜๋Š” ํƒ€์ž…
  • ๋ณดํ†ต ํ•จ์ˆ˜๊ฐ€ ์–ด๋– ํ•œ ๊ฐ’๋„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์—†๋Š” ์ƒํ™ฉ์ผ ๋•Œ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’ ํƒ€์ž…์„ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
  • ex) ๋ฌดํ•œ ๋ฃจํ”„, ์˜๋„์ ์œผ๋กœ ์—๋Ÿฌ๋ฅผ throwํ•˜๋Š” ํ•จ์ˆ˜
  • never ํƒ€์ž…์˜ ๋ณ€์ˆ˜์—๋Š” any๋ฅผ ํฌํ•จํ•ด ์–ด๋”ํ•œ ํƒ€์ž…์˜ ๊ฐ’๋„ ํ• ๋‹นํ•  ์ˆ˜ ์—†๋‹ค.
function throwError(message: string): never {
    throw new Error(message);
}

function infiniteLoop(): never {
    while (true) {
        // ๋ฌดํ•œ ๋ฃจํ”„: ํ•จ์ˆ˜๋Š” ์ ˆ๋Œ€ ์ข…๋ฃŒ๋˜์ง€ ์•Š์œผ๋ฉฐ, ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์Œ
    }
}

throwError("This is an error!"); // ํ•จ์ˆ˜๋Š” ์—๋Ÿฌ๋ฅผ ๋˜์ง€๊ณ  ์ข…๋ฃŒ๋˜์ง€ ์•Š์Œ
infiniteLoop(); // ๋ฌดํ•œ ๋ฃจํ”„์— ๋น ์ ธ ์ข…๋ฃŒ๋˜์ง€ ์•Š์Œ

 

 

ํ•œ ์ž… ํฌ๊ธฐ๋กœ ์ž˜๋ผ๋จน๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ(TypeScript) ๊ฐ•์˜ | ์ด์ •ํ™˜ Winterlood - ์ธํ”„๋Ÿฐ

drizzle96 (์—ฐ์šฐ(Ian)์˜ ๊ฐœ๋ฐœ ๋ธ”๋กœ๊ทธ) / ์ž‘์„ฑ๊ธ€ - velog

728x90