【问题标题】:Typescript: How to strongly-type a function that transforms a map of functions into similar functions with a different type param?Typescript:如何对将函数映射转换为具有不同类型参数的类似函数的函数进行强类型化?
【发布时间】:2018-11-29 06:45:14
【问题描述】:

我正在尝试强类型化 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)

【问题讨论】:

  • 对答案有什么想法吗?如果您对此有任何疑问,请告诉我,我可以改进答案。

标签: typescript redux


【解决方案1】:

globalizeSelectors 中,sliceSelectors 的类型是{[key: string]: SliceSelector&lt;TKey, T&gt; }。但在这种情况下,T 应该是谁?在您的简单版本中,T 将成为该特定切片选择器的返回类型,但是当您映射多个 T 时,不能是所有返回类型。

我将使用的解决方案是使用T 来代表sliceSelectors 的整个类型,并限制所有成员必须是SliceSelector&lt;TKey, any&gt; 类型。那里的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 不具备处理这些类型的能力。

【讨论】:

  • 嗨 Titian,对不起,我刚刚生了一个新的小儿子,我无法回答这个问题!我会好好看看这个并试一试。
  • @JamesNail 祝贺宝宝出生。这将是艰难但令人难以置信的:-)。不用担心答案,只要你得到答案,我就会在附近,:-)
  • 顺便说一句,@titian,我刚刚在这个问题上奖励了你,无论它是否有效,感谢你对此的想法!
  • @JamesNail 10x :-)。就我测试它而言,它有效。如果您有任何问题,请告诉我,我们将解决它们。
猜你喜欢
  • 1970-01-01
  • 2023-03-28
  • 2020-02-06
  • 2021-04-20
  • 2020-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-03
相关资源
最近更新 更多