小编Mac*_*ski的帖子

有没有一种方法可以传递一个泛型并推断另一个泛型?

考虑以下代码:

const selectFrom =
  <T>() =>
  <U extends Partial<T>>(fields: U) => {
    return fields as U;
  };

type Item = { a: string; b: string };

// intellisense on the argument Partial<Item>
const x = selectFrom<Item>()({ a: "" });
// typeof x => { a: string }
Run Code Online (Sandbox Code Playgroud)

selectFrom仅当返回另一个函数时,我才能获得我想要的类型。有没有办法实现以下目标签名?

// target signature:
const x = selectFrom<Item>({ a: "" });

// current signature:
const x = selectFrom<Item>()({ a: "" });
Run Code Online (Sandbox Code Playgroud)

types typescript typescript-generics

5
推荐指数
1
解决办法
900
查看次数

标签 统计

types ×1

typescript ×1

typescript-generics ×1