Skip to content

Ts体操--ReturnType #434

@zhuba-Ahhh

Description

@zhuba-Ahhh

不使用 ReturnType 实现 TypeScript 的 ReturnType<T> 泛型

type MyReturnType<T> = T extends (...args: any[]) => infer R ? R : never;

const fn = (v: boolean) => {
  if (v)
    return 1
  else
    return 2
}

type a = MyReturnType<typeof fn> // 应推导出 "1 | 2"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions