typescript - 为什么这个 Typescript 泛型类型推断失败?

标签 typescript typescript-generics

我有两个版本的数据模型(将它们称为 AB ),并且我希望能够在这些实例上执行函数,无论它们是 A 还是 B。函数本身特定于A 型和 B 型。

我已经想出了解决这个问题的方法,但它们都涉及声明通用类型(即 Functions<T> ),而我当前的设置无法轻松做到这一点。

type A = {
  a: string;
}

type B = {
  b: string;
}

type AFunctions = {
  make: () => A;
  do: (obj: A) => void;
}

type BFunctions = {
  make: () => B;
  do: (obj: B) => void;
}

type Bundle<
  Fns extends AFunctions | BFunctions,
  T extends ReturnType<Fns['make']> = ReturnType<Fns['make']>
> = {
  obj: T,
  fns: Fns,
}

function doIt<M extends AFunctions | BFunctions>(bundle: Bundle<M>) {
  bundle.fns.do(bundle.obj);
}

Typescript playground

上线bundle.fns.do(bundle.obj); ,我收到 typescript 错误:Argument of type 'ReturnType<M["make"]>' is not assignable to parameter of type 'A & B'

我期望 doIt类型安全,因为 bundle.objbundle.fns.do 上的参数类型相同。这里出了什么问题?有没有办法在不引入通用的情况下解决这个问题Functions<T>


我也可以通过添加 { type: 'a' } 来解决它和{ type: 'b' }参数分别为Bundle s,然后检查:

if (bundle.type === 'a') {
  bundle.fns.do(bundle.obj);
} else if (bundle.type === 'b') {
  bundle.fns.do(bundle.obj);
}

但这种冗余并不理想。

我认为这与缩小泛型类型的问题有关:https://github.com/microsoft/TypeScript/issues/17859

最佳答案

我认为编译器的提示是正确的。考虑以下因素:

let
func : AFunctions | BFunctions = {
  'make' : function() : A { return {'a': "A"} },
  'do' : function(_ : A) { }
},
someB : B = { 'b' : "B" },
bundle : Bundle<AFunctions | BFunctions> = {
  'obj' : someB,
  'fns' : func,
}

此类型检查,但 'do' 显然不能使用参数 'obj' 进行调用。根本问题是,在 bundle 中,类型 T 被推断为 A | B,而不是基于 make 类型的 AB,正如我认为您所期望的那样。

目前还不清楚您想要实现什么目标。特别是,不清楚为什么不能声明泛型类型,因为这似乎正是您所需要的:

type GenericBundle<X> = { obj : X, do : (obj : X) => void };
type AFunctions = GenericBundle<A>;
type BFunctions = GenericBundle<B>;
type Bundle = AFunctions | BFunctions;

function doIt<X>(bundle: GenericBundle<X>) {
  bundle.do(bundle.obj);
}

let
someA : A = { 'a' : "A" },
someB : B = { 'b' : "B" },
bundle1 : Bundle = {
  'obj' : someA,
  'do' : function(_ : A) { },
},
bundle2 : Bundle = {
  'obj' : someB,
  'do' : function(_ : B) { },
},
bundle3_wrong : Bundle = { // doesn't typecheck
  'obj' : someA,
  'do' : function(_ : B) { },
},
bundle4_wrong : Bundle = { // doesn't typecheck
  'obj' : someB,
  'do' : function(_ : A) { },
};

doIt(bundle1);
doIt(bundle2);

关于typescript - 为什么这个 Typescript 泛型类型推断失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57878261/

相关文章:

node.js - 使用 Angular 2 observables 异步填充对象属性?

typescript - 未推断可选泛型类型

typescript - 使用类型作为具有约束的泛型的别名

angular - 声明具有通用类型的组件

TypeScript:对象类型的索引签名隐式具有 'any' 类型

javascript - 如何在 Nestjs 的服务中获取 req.user

typescript - 如何输入对应类型的元组数组?

typescript - 禁止显式类型参数或约束联合类型参数以提高类型安全性

TypeScript:元素隐式具有 'any' 类型,因为类型 'string' 的表达式不能用于索引类型

generics - 如何使通用类型可选?