打字稿:缩小映射类型不适用于泛型

时间:2021-02-18 10:27:46

标签: javascript typescript typescript-generics

问题:我想编写一个以对象和属性名称作为参数的函数。我想要的是 实现的是只接受具有特定类型的属性的属性名称。

示例:在一个人身上 对象我有字段名称:字符串和年龄:编号,然后我的函数应该只能用参数调用 (人,'名字')。这可以通过创建这种类型来实现:

export type OnlyPropertiesWithSpecificType<TYPE, T> = {
  [K in keyof T]: T[K] extends TYPE ? K : never;
}[keyof T];

在函数内部访问属性时,属性值的类型应该像这样约束:

type Person = {
  name: string;
  age: number;
};

function somePersonFunction(obj: Person, param: OnlyPropertiesWithSpecificType<string, Person>): string {
  return obj[param]; // works, obj[param] is of type 'string'
}

但是,当我尝试对函数进行泛化时,它不再受类型限制:

function someGenericFunction<T>(obj: T, param: OnlyPropertiesWithSpecificType<string, T>): string {
  return obj[param]; // doesn't work: "TS2322: Type 'T[{ [K in keyof T]: T[K] extends string ? K : never; }[keyof T]]' is not assignable to type 'string'."
}

这令人困惑,因为编译器仍然只接受属于 'string' 类型的属性的属性名称作为参数:

someGenericFunction(person, 'name'); // works
someGenericFunction(person, 'age'); // doesn't work

我尝试了什么:

  • TS 版本 3.4.5 和 4.1.2。
  • T 的各种变体,即 T 扩展对象

我用上面的例子创建了一个沙箱:https://codesandbox.io/s/typescript-forked-ypy0b

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

看起来 TS 还没有算出 someGenericFunction 总是会返回一个 string,尽管在实践中它总是会。

但是,除非您绝对需要 : stringsomeGenericFunction 返回类型注释,否则您可以省略它,您的代码将按预期工作。

function someGenericFunction<T>(
  obj: T,
  param: OnlyPropertiesWithSpecificType<string, T>
) {
  return obj[param]; // inferred return type is T[OnlyPropertiesWithSpecificType<string, T>]
}

当使用实际类型调用时,TS 确实推断出 T[OnlyPropertiesWithSpecificType<string, T>] 将始终可分配给 string,这就是函数起作用的原因。