Typescript 메서드 오버로드가 다른 언어와 다른 이유

Typescript

Posted by jopemachine on February 15, 2021 Updated on November 29, 2021

Typescript 메서드 오버로드가 다른 언어와 다른 이유

타입스크립트는 메서드 오버로드를 지원하지 않는다.

다른 언어처럼 오버로드한다면 타입스크립트를 자바스크립트로 바꾸어 실행할 때, 오버로드 했던 함수들이 같은 헤더를 갖게 된다.

자바스크립트에서 함수 변수는 단순히 함수 포인터일 뿐으로, 오버로딩이 불가능하다.

그래서 타입스크립트에서의 메서드 오버로드는 구현 없이 헤더만 여럿 선언해 놓고

하나의 구현부에서 수동으로 매개 변수를 검사하는 식으로 작성해야 한다.

메서드 오버로드 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  public connect(path: string, connectionListener?: GenericFunction): Socket;

  public connect(
    port: number,
    host: string,
    connectionListener?: GenericFunction
  ): Socket;

  public connect(
    options: TcpSocketConnectOpts | IpcSocketConnectOpts,
    connectionListener?: GenericFunction
  ): Socket;

  public connect(arg1: any, arg2?: any, arg3?: any) {
    let connectionPromise: Promise<Deno.Conn>;
    let path, port, host, options, connectionListener: any;

    // * arg1: port
    // * arg2: host
    // * arg3: connectionListener
    if (typeof arg1 === "number" && arg2 && typeof arg2 === "string") {
      port = arg1;
      host = arg2;
      connectionListener = arg3;

      ...
    }

    // handle 'path', 'connectionListener'
    else if (typeof arg1 === "string" && arg2 && !arg3) {
      path = arg1;
      connectionListener = arg2;

      ...
    }

    // handle 'options', 'connectionListener'
    else {
      options = arg1;
      connectionListener = arg2;
      ...
    }

출처