๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ

1. type

type PeopleType = {
  name: string
  age: number
}
type WindowStates = "open" | "closed" | "minimized";
type LockStates = "locked" | "unlocked";
type PositiveOddNumbersUnderTen = 1 | 3 | 5 | 7 | 9;
type Dog = {
  name: string;
  tail?: {
    shakeTail?: () => string;
  };

  hasMaster?: boolean;
};

function sayHi(dog: Dog): string {
  return dog.tail?.shakeTail?.();
}

Type์˜ property๋ฅผ optionalํ•˜๊ฒŒ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

2. interface

  • ์ฃผ๋กœ ๊ฐ์ฒด์˜ value ํƒ€์ž…์„ ์ •์˜ํ•œ๋‹ค.
  • ์ธํ„ฐํŽ˜์ด์Šค ์ด๋ฆ„์•ž์— ๋Œ€๋ฌธ์ž๋กœ ์“ด๋‹ค.
  • type๊ณผ ๋‹ค๋ฅด๊ฒŒ ์„ ์–ธ์  ํ™•์žฅ(๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ํ™•์žฅ ์„ ์–ธ)์ด ๋œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.
interface User {
  name: string;
  id: number;
  addComment: (comment:string) => string;
}

์ •์˜ํ•œ interface์˜ ํƒ€์ž…์„ user ์— ์ ์šฉํ•œ๋‹ค.

const user: User = {
  name: "Hayes",
  id: 0,
  addComment: (comment) => `This is ${comment}`
};

์ธํ„ฐํŽ˜์ด์Šค ํ™•์žฅ ์˜ˆ์‹œ

interface ButtonInterface {
  readonly _type:string;
  width?:number;
  height?:number;
  onInit?():void;
  onClick():void;
}

// ButtonInterface๋ฅผ ํ™•์žฅํ•˜๋Š” ToggleButtonInterface
interface ToggleButtonInterface extends ButtonInterface {
  toggle():void;
  onToggled?():void;
}

// ButtonInterface๋ฅผ ํ™•์žฅํ•˜๋Š” CounterButtonInterface
interface CounterButtonInterface extends ButtonInterface {
  increase():void;
  decrease():void;
  onIncreased?():void;
  onDecreased?():void;
}

 

3. Generic

 

Generic์ด๋ž€ ๋ฐ์ดํ„ฐ์˜ ํƒ€์ž…์„ ์ผ๋ฐ˜ํ™”ํ•œ๋‹ค(generalize)ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค. ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ํƒ€์ž…์— ๋Œ€ํ•ด ํ˜ธํ™˜์„ ๋งž์ถฐ์•ผ ํ•˜๋Š” ์ƒํ™ฉ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ๋ฒ•์ด๋‹ค. Generic์€ ์ž๋ฃŒํ˜•์„ ์ •ํ•˜์ง€ ์•Š๊ณ  ์—ฌ๋Ÿฌ ํƒ€์ž…์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค. ์ฆ‰, ์„ ์–ธ ์‹œ์ ์ด ์•„๋‹ˆ๋ผ ์ƒ์„ฑ ์‹œ์ ์— ํƒ€์ž…์„ ๋ช…์‹œํ•˜์—ฌ ํ•˜๋‚˜์˜ ํƒ€์ž…๋งŒ์ด ์•„๋‹Œ ๋‹ค์–‘ํ•œ ํƒ€์ž…์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ธฐ๋ฒ•์ด๋‹ค. ํ•œ๋ฒˆ์˜ ์„ ์–ธ์œผ๋กœ ๋‹ค์–‘ํ•œ ํƒ€์ž…์— '์žฌ์‚ฌ์šฉ'์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค. ์ œ๋„ค๋ฆญ์„ ์“ฐ์ง€ ์•Š์„ ๊ฒฝ์šฐ, ๋ถˆํ•„์š”ํ•œ ํƒ€์ž… ๋ณ€ํ™˜์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ”„๋กœ๊ทธ๋žจ์˜ ์„ฑ๋Šฅ์— ์•…์˜ํ–ฅ์„ ๋ฏธ์น˜๊ธฐ๋„ ํ•˜๋Š”๋ฐ, ์ œ๋„ค๋ฆญ์„ ์‚ฌ์šฉํ•˜๊ฒŒ๋˜๋ฉด ๋”ฐ๋กœ ํƒ€์ž… ๋ณ€ํ™˜์„ ํ•  ํ•„์š”๊ฐ€ ์—†์–ด์„œ ํ”„๋กœ๊ทธ๋žจ์˜ ์„ฑ๋Šฅ์ด ํ–ฅ์ƒ๋˜๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.

 

function sort<T>(items:T[]) :T[] {
	return items.sort();
}

const nums :number[] = [1,2,3,4];
const chars :string[] = ["a", "b", "c", "d"];

sort<number>(nums);
sort<string>(chars);

 

generic์„ ์“ฐ์ง€ ์•Š๋Š”๋‹ค๋ฉด,

1) ํƒ€์ž…์„ ๋ฏธ๋ฆฌ ์ง€์ •ํ•˜๊ฑฐ๋‚˜, 2) any๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

1) ํ™•์‹คํ•œ ํƒ€์ž…์ฒดํฌ๊ฐ€ ์ด๋ค„์งˆ ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ ํ•ญ์ƒ number๋ผ๋Š” ํƒ€์ž…์„ ๋ฐ›์•„์•ผํ•˜๋ฏ€๋กœ ๋ฒ”์šฉ์„ฑ์ด ๋–จ์–ด์ง„๋‹ค.
2) any๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ž๋ฃŒ์˜ ํƒ€์ž…์„ ์ œํ•œํ•  ์ˆ˜ ์—†๊ณ , ์ด function์„ ํ†ตํ•ด ์–ด๋–ค ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฆฌํ„ด๋˜๋Š”์ง€ ์•Œ ์ˆ˜ ์—†๋‹ค.

generic์„ ์“ด๋‹ค๋ฉด,

1) ์ œ๋„ค๋ฆญ์˜ ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์กฐ

2) ๋ฐฐ์—ด์˜ ํ˜•ํƒœ๋ฅผ ๋ฐ›์„ ๋•Œ

 

3) ๋‘ ๊ฐœ ์ด์ƒ์˜ Generic ์‚ฌ์šฉํ•˜๊ธฐ

๋‘ ๋ณ€์ˆ˜์˜ ํƒ€์ž…์ด ๋‹ค๋ฅผ ๊ฒฝ์šฐ ๋‘ ๊ฐ€์ง€์˜ ํƒ€์ž… ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. T ๋‹ค์Œ์œผ๋กœ ์˜ค๋Š” ์•ŒํŒŒ๋ฒณ์ธ U๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

4) ๋‘ ๊ฐœ ์ด์ƒ์˜ Generic ์‚ฌ์šฉํ•˜๊ธฐ

extends์™€ ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ์กฐ๊ฑด๋ถ€ ํƒ€์ž…์œผ๋กœ๋„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

4. ์ฝ”๋“œ ์Šคํƒ€์ผ(Props-suffix)

When using props we name props usually according to the component we’re writing, with a Props-suffix.

type GreetingProps = {
  name: string;
};

function Greeting(props: GreetingProps) {
  return <p>Hi {props.name} ๐Ÿ‘‹</p>
}

 

Destructuring ์ฝ”๋“œ ์Šคํƒ€์ผ์„ ์ž‘์—…ํ•˜์—ฌ ๊ฐ€๋…์„ฑ์„ ๋”์šฑ ๋†’์ธ๋‹ค.

function Greeting({ name }: GreetingProps) {
  return <p>Hi {name} ๐Ÿ‘‹</p>;
}
๋ฐ˜์‘ํ˜•