【问题标题】:How to use getFormInitialValues selector with TypeScript properly?如何正确使用带有 TypeScript 的 getFormInitialValues 选择器?
【发布时间】:2018-08-24 12:43:35
【问题描述】:

找到了这个解决方案,但我认为这是一个 hack:

export const getInitialValues = getFormInitialValues('formName') as (state: State) => FormValues;

在 TypeScript 中使用 getFormInitialValues 的正确解决方案是什么?

【问题讨论】:

    标签: typescript redux-form


    【解决方案1】:

    type declaration for getFormInitialValues

    export const getFormInitialValues: DataSelector;
    

    地点:

    export type DataSelector<FormData = {}, State = {}> = (formName: string, getFormState?: GetFormState) => (state: State) => FormData;
    

    看起来DataSelector 可能是一个泛型函数类型,所以你可以像getFormInitialValues&lt;FormValues, State&gt;('formName') 一样调用它,但它被定义为一个泛型类型别名,其中类型参数设置为它们的默认值。正确的定义是:

    export type DataSelector = <FormData = {}, State = {}>(formName: string, getFormState?: GetFormState) => (state: State) => FormData;
    

    我看到错误是在this PR 中引入的。考虑提交 PR 以更正文件中的所有类型别名。同时,您可以将类型的副本添加到您的项目中,修改它们,并将它们注册到package.jsonlike this

    【讨论】:

      猜你喜欢
      • 2022-01-17
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 2022-11-29
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      • 2019-12-19
      相关资源
      最近更新 更多