为什么这个 Typescript 泛型类型推断会失败?
Why does this Typescript generic type inference fail?
我有 2 个版本的数据模型(称它们为 A
和 B
),我希望能够在这些实例上执行函数,无论它们是 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);
}
在 bundle.fns.do(bundle.obj);
行,我收到打字稿错误:Argument of type 'ReturnType<M["make"]>' is not assignable to parameter of type 'A & B'
我希望 doIt
是类型安全的,因为 bundle.obj
与 bundle.fns.do
上的参数类型相同。这里出了什么问题?有没有办法在不引入通用的情况下解决这个问题 Functions<T>
?
我也可以通过在各自的Bundle
中添加{ type: 'a' }
和{ type: 'b' }
参数来解决,然后检查:
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
不确定这是否会满足您的用例,但也许会有所帮助 - playground link
此处的缺点是 do::obj 参数上的 'any'(可以是 'T | any' 或只是 'any'),这对您来说可能重要也可能不重要。
我认为编译器的抱怨是对的。考虑以下因素:
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
中,根据 make
的类型,类型 T
被推断为 A | B
,而不是 A
或 B
,正如我认为您所期待的那样。
不太清楚你想要达到什么目的。特别是,不清楚为什么不能声明泛型类型,因为这似乎正是您所需要的:
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);
我有 2 个版本的数据模型(称它们为 A
和 B
),我希望能够在这些实例上执行函数,无论它们是 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);
}
在 bundle.fns.do(bundle.obj);
行,我收到打字稿错误:Argument of type 'ReturnType<M["make"]>' is not assignable to parameter of type 'A & B'
我希望 doIt
是类型安全的,因为 bundle.obj
与 bundle.fns.do
上的参数类型相同。这里出了什么问题?有没有办法在不引入通用的情况下解决这个问题 Functions<T>
?
我也可以通过在各自的Bundle
中添加{ type: 'a' }
和{ type: 'b' }
参数来解决,然后检查:
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
不确定这是否会满足您的用例,但也许会有所帮助 - playground link
此处的缺点是 do::obj 参数上的 'any'(可以是 'T | any' 或只是 'any'),这对您来说可能重要也可能不重要。
我认为编译器的抱怨是对的。考虑以下因素:
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
中,根据 make
的类型,类型 T
被推断为 A | B
,而不是 A
或 B
,正如我认为您所期待的那样。
不太清楚你想要达到什么目的。特别是,不清楚为什么不能声明泛型类型,因为这似乎正是您所需要的:
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);