[TIL] TypeScript (2)

Sooยท2022๋…„ 12์›” 16์ผ
0
post-thumbnail

Keyof Type Operator

๊ฐ์ฒด ์œ ํ˜•์„ ์‚ฌ์šฉํ•˜์—ฌ ํ‚ค์˜ ๋ฌธ์ž์—ด ๋˜๋Š” ์ˆซ์ž ๋ฆฌํ„ฐ๋Ÿด ๊ฒฐํ•ฉ์„ ์ƒ์„ฑํ•œ๋‹ค.

type Point = {x: number; y: number};
type P = keyof Point; // x | y

๐Ÿ’ก ์งš๊ณ  ๋„˜์–ด๊ฐ€๊ธฐ

  • object์— ์‚ฌ์šฉํ•˜๋ฉด object๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ชจ๋“  key ๊ฐ’์„ union type์œผ๋กœ ํ•ฉ์ณ์„œ ๋‚ด๋ณด๋‚ด์ค€๋‹ค.
  • object์— ๋“ค์–ด์žˆ๋Š” key๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ƒˆ๋กœ์šด ํƒ€์ž…์„ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์—ฐ์‚ฐ์ž์ด๋‹ค.
type IndexSignature = {
    [k: string]: number;
}

type Index = keyof IndexSignature // string | number

const obj: IndexSignature = {
    a: 1,
    b: 2
};

๐Ÿ’ก ์งš๊ณ  ๋„˜์–ด๊ฐ€๊ธฐ

  • JavaScript ์˜ค๋ธŒ์ ํŠธํ‚ค๊ฐ€ ํ•ญ์ƒ ๋ฌธ์ž์—ด์— ๊ฐ•์ œ๋˜๊ธฐ ๋•Œ๋ฌธ์— obj[0]์€ ํ•ญ์ƒ obj[โ€™0โ€™]๊ณผ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

Typeof Type Operator

  • JavaScript์—์„œ๋„ ์ด๋ฏธ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์—ฐ์‚ฐ์ž
  • ํ•ด๋‹น ๋ณ€์ˆ˜์˜ type์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ์—ฐ์‚ฐ์ž
let s = 'hello';

console.log(typeof s) // string

๋‹ค๋ฅธ ํƒ€์ž… ์—ฐ์‚ฐ์ž์™€ ์กฐํ•ฉํ•˜์—ฌ Typeof๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์–‘ํ•œ ํŒจํ„ด์„ ์‰ฝ๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

type Predicate = (x: unknown) => boolean;
type K = ReturnType<Predicate>; // boolean;

๐Ÿ’ก ์งš๊ณ  ๋„˜์–ด๊ฐ€๊ธฐ

  • ReturnType: ํƒ€์ž…์ด๋‚˜ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ํƒ€์ž…์œผ๋กœ ์ •์˜ํ•œ๋‹ค.
function f() {
    return {x: 10, y: 3};
}

type P = ReturnType<typeof f>
// type P = {x: number, y: number};

๐Ÿ’ก ์งš๊ณ  ๋„˜์–ด๊ฐ€๊ธฐ

  • ๋งŒ์•ฝ์— ํ•จ์ˆ˜์—๋„ ReturnType์„ ์ ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด! ํ•จ์ˆ˜๋ช…์„ ๋„ฃ์ง€๋ง๊ณ  ํ•จ์ˆ˜์— typeof๋ฅผ ์ ์šฉํ•ด์•ผ ํ•œ๋‹ค.

์ œํ•œ์‚ฌํ•ญ

TypeScript๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์‹์˜ ์ข…๋ฅ˜๋ฅผ ์˜๋„์ ์œผ๋กœ ์ œํ•œํ•œ๋‹ค.

let shouldContinue: typeof msgbox("Are you sure you want to continue?");

Generics

โ€˜์žฌ์‚ฌ์šฉโ€™ ๊ฐ€๋Šฅํ•œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋„๊ตฌ ์ค‘ ํ•˜๋‚˜

๐Ÿ’ก ์งš๊ณ  ๋„˜์–ด๊ฐ€๊ธฐ

  • ๋‹จ์ผ ํƒ€์ž…์ด ์•„๋‹Œ ๋‹ค์–‘ํ•œ ํƒ€์ž…์—์„œ ์ž‘๋™ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์‚ฌ์šฉ์ž๋Š” Generic์„ ํ†ตํ•ด ์—ฌ๋Ÿฌ ํƒ€์ž…์˜ ์ปดํฌ๋„ŒํŠธ๋‚˜ ์ž์‹ ๋งŒ์˜ ํƒ€์ž…์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์„ ์–ธ ์‹œ์ ์ด ์•„๋‹ˆ๋ผ ์ƒ์„ฑ ์‹œ์ ์— ํƒ€์ž…์„ ๋ช…์‹œํ•˜์—ฌ ํ•˜๋‚˜์˜ ํƒ€์ž…๋งŒ์ด ์•„๋‹Œ ๋‹ค์–‘ํ•œ ํƒ€์ž…์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ธฐ๋ฒ•

Generic ๊ธฐ๋ณธ ๋ฌธ๋ฒ•

function identity<T>(arg: T): T{
    return arg;
}

identity<string>('Hello World!');
identity('Hello World!'); // ํƒ€์ž… ์ถ”๋ก 
  • T๋Š” Type์˜ ์•ฝ์ž๋กœ Generic์„ ์„ ์–ธํ•  ๋•Œ T(ํƒ€์ž… ๋ณ€์ˆ˜)๋ฅผ ๊ด€์šฉ์ ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

  • ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑ(ํ˜ธ์ถœ)๋˜๋Š” ์‹œ์ ์— string ํƒ€์ž…์ด ๋ช…์‹œ๋œ๋‹ค.

  • ๋˜ํ•œ paremeter์— ๋Œ€ํ•œ ํƒ€์ž…๊ณผ return์— ๋Œ€ํ•œ ํƒ€์ž…์ด ๋™์ผํ•œ์ง€ ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ’ก ์งš๊ณ  ๋„˜์–ด๊ฐ€๊ธฐ
Generic์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์ด์œ 

  • Generic์ด ์—†๋‹ค๋ฉด ํƒ€์ž…์„ ๋ฏธ๋ฆฌ ์ง€์ •ํ•˜๊ฑฐ๋‚˜, any๋ฅผ ์ด์šฉํ•ด์„œ ํƒ€์ž…์„ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.
  • ํƒ€์ž…์„ ๋ฏธ๋ฆฌ ์ง€์ •ํ•˜๊ฒŒ ๋˜๋ฉด ๋ฒ”์šฉ์„ฑ์ด ๋–จ์–ด์ง„๋‹ค.
    any๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํƒ€์ž…์„ ์ œํ•œํ•  ์ˆ˜ ์—†๊ณ , parameter์™€ return์˜ ํƒ€์ž…์„ ํ™•์ธํ•  ์ˆ˜ ์—†๋‹ค.
  • ์žฌ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜์ง€ ์•Š๊ฑฐ๋‚˜, ํƒ€์ž…์— ๋Œ€ํ•œ ๊ฒ€์ฆ์„ ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— Generic์„ ์‚ฌ์šฉํ•œ๋‹ค.

์ œ๋„ค๋ฆญ ํƒ€์ž… ๋ณ€์ˆ˜ ์ž‘์—… (Working with Generic Type Variables)

function identity<T>(arg: T): T {
    console.log(arg.length); // error ๋ฐœ์ƒ
    return arg;
}

๐Ÿ’ก ์งš๊ณ  ๋„˜์–ด๊ฐ€๊ธฐ

  • ๋งŒ์•ฝ ํƒ€์ž…๋ณ€์ˆ˜ T์— ๋ฐฐ์—ด์ด ๋“ค์–ด์™”๋‹ค๋ฉด error๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์ง€๋งŒ
    ํƒ€์ž…๋ณ€์ˆ˜ T๋Š” ์ •ํ•ด์ง€์ง€ ์•Š์€ ํƒ€์ž…์ด๊ธฐ ๋•Œ๋ฌธ์— .length ํ”„๋กœํผํ‹ฐ๊ฐ€ ์œ ํšจํ•˜๋‹ค๊ณ  ํŒ๋‹จํ•  ์ˆ˜ ์—†๋‹ค.
  • T(ํƒ€์ž… ๋ณ€์ˆ˜)์— ๋Œ€ํ•œ ์„ธ๋ถ€์ ์ธ ๋ณ€์ˆ˜ ํƒ€์ž…์„ ์ง€์ •ํ•œ๋‹ค.
function identity<T>(arg: T[]): T[] {
    console.log(arg.length);
    return arg;
}
  • Generic ํ•จ์ˆ˜ identity๋Š” ํƒ€์ž…๋ณ€์ˆ˜ T์™€ T ๋ฐฐ์—ด ์ธ์ˆ˜ arg๋ฅผ ์ทจํ•˜๊ณ  T ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ œ๋„ค๋ฆญ ํƒ€์ž… (Generic Types)

  • ํ•จ์ˆ˜ ์ž์ฒด์˜ Type๊ณผ Generic Interface๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
function identity<T>(arg: T): T {
 return arg;
}

let myIdentity: <T>(arg: T) => T = identity;
let myIdentity: { <T>(arg: T): T } = identity;
  • ์ œ๋„ค๋ฆญ ํƒ€์ž…์„ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…์˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ๊ทธ๋‹ˆ์ฒ˜๋กœ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

์ œ๋„ค๋ฆญ ์ธํ„ฐํŽ˜์ด์Šค (Generic Interface)

interface GenericIdentity<T> {
    (arg: T): T;
}

function identity<T>(arg: T): T {
    return arg;
}

let checkIdentity: GenericIdentity = identity;
let checkIdentity: GenericIdentity<number> = identity; // type ๊ฐ•์กฐ
  • ๋งŒ์•ฝ interface์— type์„ ๊ฐ•์กฐํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•˜๋‹ค. (number)

์ œ๋„ค๋ฆญ ํด๋ž˜์Šค (Generic Class)

class GenericMath<T> {
    pi: T;
    sum: (x: T, y: T) => T;
}

let math = new GenericMath<number>();

math.pi = 3.14;
math.sum = (x, y) => {return x + y};
  • ์ œ๋„ค๋ฆญ ์ธํ„ฐํŽ˜์ด์Šค์™€ ๋น„์Šทํ•œ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

์ œ๋„ค๋ฆญ ์ œ์•ฝ์กฐ๊ฑด (Generic Constraints)

  • ํŠน์ • ํƒ€์ž…๋“ค๋กœ๋งŒ ๋™์ž‘ํ•˜๋Š” ์ œ๋„ค๋ฆญ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
function identity<T>(arg: T): T {
    console.log(arg.length); // error ๋ฐœ์ƒ
    return arg;
}
  • ์ œ์•ฝ์กฐ๊ฑด ๋ช…์‹œ
interface Lengthwise {
    length: number;
}

function identity<T extends Lengthwise>(arg: T): T {
    console.log(arg.length)
    return arg;
}

identity("string")
identity({ length: 10, value: 3})

๐Ÿ’ก ์งš๊ณ  ๋„˜์–ด๊ฐ€๊ธฐ

  • ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด ์ œ์•ฝ์กฐ๊ฑด์„ ๋ช…์‹œํ•  ์ˆ˜ ์žˆ๋‹ค.
  • extends๋ฅผ ์‚ฌ์šฉํ•ด์„œ type์„ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.
  • .length ํ”„๋กœํผํ‹ฐ๊ฐ€ ์—†๋Š” type์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด length์— ๋Œ€ํ•œ ๋ช…์‹œ๋ฅผ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.
profile
Soogineer's Devlog

0๊ฐœ์˜ ๋Œ“๊ธ€