Typescript overload arrow functions

前端 未结 3 453
夕颜
夕颜 2020-12-08 19:50

So we can do:

export function myMethod (param: number) :number
export function myMethod (param: string) :string

export function myMethod (param: string | nu         


        
3条回答
  •  萌比男神i
    2020-12-08 20:27

    This solution is based on @Sam96 but keeps the code of the arrow function fully typed.

    type Create = {
      (): Vector<0>;
      (x: T): Vector;
      (x: T, y: T): Vector;
    };
    
    const create: Create = (
      x?: T,
      y?: T
    ) => ({
      x: x ?? 0,
      y: y ?? x ?? 0,
    });
    

提交回复
热议问题