제네릭 타입 지정 과정

조민호·2023년 1월 22일
0

제네릭은 제네릭을 선언한 부분에 대입되는 변수의 타입을 통해 타입이 정해지게 되는 것을 알 수 있습니다

즉, 제네릭이 있는 그 위치에 들어가는 타입에 따라 제네릭의 타입이 정해지는 것입니다


  • 일반적인 변수의 경우
type Check<T> = T;
type stringType = Check<string>; 
type numberType = Check<number>; 
type valueType = Check<'a'>;
type funcType = Check<() => string>;

const stringTest: stringType = 'hello';

const numberTest: numberType = 123;

const valueTest: valueType = 'a';

const funcTest: funcType = function () {
  return 'hello';
};

여기서 좀 주위해야 할 것은

valueTest의 타입은 ‘a’가 됩니다 string타입이 아닙니다!

그래서 값으로 ‘a’만 가능합니다


funcTest의 타입은 리턴타입이 string인 function입니다

  • 함수의 경우에는 함수의 인자로 넘겨주는 값의 타입에 따라 제네릭의 타입이 지정되고
    function chekNotNull<T>(arg: T | null): T {
    	...
    }
    
    // 123이 들어가서 T의 타입은 number로 지정
    const result = chekNotNull(123); 
  • 클래스 , 인터페이스의 경우에는
    • 생성자로 넘겨주는 값으로 제네릭 타입을 선언하던가
      
      interface Either<L, R> {
        left: () => L;
        right: () => R;
      }
      
      class SimpleEither<L, R> implements Either<L, R> {
      
        constructor(private leftValue: L, private rightValue: R) {}
      
      	...
      }
      
      const either= new SimpleEither(4, 5);
      // 4 5 가 들어가서 클래스의 L과R의 타입이 지정되고
      // 동시에 인터페이스의 L,R도 지정됨 (같은 L,R이므로)
    • 따로 지정해 주던가
      interface Stack<T> {
        readonly size: number;
        push(num: T): void;
        pop(): T;
      }
      
      class StackImpl<T> implements Stack<T> {
      
        //스택의 전체 크기
        constructor(private capacity: number) {}
      
      	...
      }
      
      const stack2: Stack<**number**> = new StackImpl<**number**>(10);
      // number 가 들어가서 클래스의 T의 타입이 지정되고
      // 동시에 인터페이스의 T에도 number로 지정됨 (같은 T 이므로)
profile
웰시코기발바닥

0개의 댓글