我正在尝试强类型化一个 globalizeSelectors
函数,该函数将转换 redux 选择器函数的映射,以便它们接受 GlobalState
类型而不是它们的 StateSlice 类型基于其 StateSlice 的键(其中 StateSlice 表示它是 GlobalState 对象的属性之一的值)。
棘手的部分是选择器的返回类型可能全都不同,而且我不太清楚如何输入这种变体(或者如果可能的话)。根据 typescript 文档,我猜这可能涉及对 infer
运算符的一些巧妙使用,但我的 typescript-fu 还没有达到那个水平。
到目前为止,这是我得到的: (顺便说一句,对于你的 reduxy 类型,不要介意这些选择器不处理 props 或额外的 args 的事实——我已经删除了它以简化这一点)
import { mapValues } from 'lodash'
// my (fake) redux state types
type SliceAState = { name: string }
type SliceBState = { isWhatever: boolean }
type GlobalState = {
a: SliceAState;
b: SliceBState;
}
type StateKey = keyof GlobalState
type Selector<TState, TResult> = (state: TState) => TResult
type StateSlice<TKey extends StateKey> = GlobalState[TKey]
type GlobalizedSelector<TResult> = Selector<GlobalState, TResult>
const globalizeSelector = <TKey extends StateKey, Result>(
sliceKey: TKey,
sliceSelector: Selector<StateSlice<TKey>, Result>
): GlobalizedSelector<Result> => state => sliceSelector(state[sliceKey])
// an example of a map of selectors as they might be exported from their source file
const sliceASelectors = {
getName: (state: SliceAState): string => state.name,
getNameLength: (state: SliceAState): number => state.name.length
}
// fake global state
const globalState: GlobalState = {
a: { name: 'My Name' },
b: { isWhatever: true }
}
// so this works...
const globalizedGetName = globalizeSelector('a', sliceASelectors.getName)
const globalizedNameResult: string = globalizedGetName(globalState)
const globalizedGetNameLength = globalizeSelector(
'a',
sliceASelectors.getNameLength
)
const globalizedNameLengthResult: number = globalizedGetNameLength(globalState)
/* but when I try to transform the map to globalize all its selectors,
I get type errors (although the implementation works as untyped
javascript):
*/
type SliceSelector<TKey extends StateKey, T> = T extends Selector<
StateSlice<TKey>,
infer R
>
? Selector<StateSlice<TKey>, R>
: never
const globalizeSelectors = <TKey extends StateKey, T>(
sliceKey: TKey,
sliceSelectors: {
[key: string]: SliceSelector<TKey, T>;
}
) => mapValues(sliceSelectors, s => globalizeSelector(sliceKey, s))
const globalized = globalizeSelectors('a', sliceASelectors)
/*_________________________________________^ TS Error:
Argument of type '{ getName: (state: SliceAState) => string; getNameLength: (state: SliceAState) => number; }' is not assignable to parameter of type '{ [key: string]: never; }'.
Property 'getName' is incompatible with index signature.
Type '(state: SliceAState) => string' is not assignable to type 'never'. [2345]
*/
const globalizedGetName2: string = globalized.getName(globalState)
最佳答案
在globalizeSelectors
类型 sliceSelectors
是{[key: string]: SliceSelector<TKey, T> }
.但是谁应该T
在这种情况下?在您的简单版本中 T
将成为该特定切片选择器的返回类型,但是当您映射多个 T
时不能是所有的返回类型。
我会使用的解决方案是使用 T
重现 sliceSelectors
的整个类型限制所有成员的类型必须为 SliceSelector<TKey, any>
. any
那里只是代表我们不关心切片选择器的返回类型是什么。
即使我们不关心每个切片选择器的返回类型是什么,T
将准确地捕获类型(即对象中每个函数的返回类型将不是 any
而是实际类型)。然后我们可以使用 T
创建一个映射类型,使对象中的每个函数全局化。
import { mapValues } from 'lodash'
// my (fake) redux state types
type SliceAState = { name: string }
type SliceBState = { isWhatever: boolean }
type GlobalState = {
a: SliceAState;
b: SliceBState;
}
type StateKey = keyof GlobalState
type GlobalizedSelector<TResult> = Selector<GlobalState, TResult>
const globalizeSelector = <TKey extends StateKey, Result>(
sliceKey: TKey,
sliceSelector: Selector<StateSlice<TKey>, Result>
): GlobalizedSelector<Result> => state => sliceSelector(state[sliceKey])
// an example of a map of selectors as they might be exported from their source file
const sliceASelectors = {
getName: (state: SliceAState): string => state.name,
getNameLength: (state: SliceAState): number => state.name.length
}
// fake global state
const globalState: GlobalState = {
a: { name: 'My Name' },
b: { isWhatever: true }
}
type Selector<TState, TResult> = (state: TState) => TResult
type StateSlice<TKey extends StateKey> = GlobalState[TKey]
// Simplified selctor, not sure what the conditional type here was trying to achive
type SliceSelector<TKey extends StateKey, TResult> = Selector<StateSlice<TKey>, TResult>
const globalizeSelectors = <TKey extends StateKey, T extends {
[P in keyof T]: SliceSelector<TKey, any>
}>(
sliceKey: TKey,
sliceSelectors: T
) : { [P in keyof T]: GlobalizedSelector<ReturnType<T[P]>> } => mapValues(sliceSelectors, s => globalizeSelector(sliceKey, s as any)) as any // Not sure about mapValues
const globalized = globalizeSelectors('a', sliceASelectors)
const globalizedGetName2: string = globalized.getName(globalState)
唯一的小问题是 mapValues
需要一些类型断言才能工作,我不认为 mapValues
具备处理这些类型的能力。
关于 typescript :如何对将函数映射转换为具有不同类型参数的类似函数的函数进行强类型化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53533278/