小编dar*_*rpa的帖子

获取函数/类构造函数的参数类型

我正在尝试做一些我不确定在TypeScript中可能做的事情:从函数中推断出参数类型/返回类型.

例如

function foo(a: string, b: number) {
  return `${a}, ${b}`;
}

type typeA = <insert magic here> foo; // Somehow, typeA should be string;
type typeB = <insert magic here> foo; // Somehow, typeB should be number;
Run Code Online (Sandbox Code Playgroud)

我的用例是尝试创建一个包含构造函数和参数的配置对象:

例如:

interface IConfigObject<T> {
    // Need a way to compute type U based off of T.
    TypeConstructor: new(a: U): T;
    constructorOptions: U;
}

// In an ideal world, could infer all of this from TypeConstructor

class fizz {
    constructor(a: number) {} …
Run Code Online (Sandbox Code Playgroud)

typescript

21
推荐指数
4
解决办法
8647
查看次数

标签 统计

typescript ×1