减速器中的打字稿类型问题

克莱门特·蒙托瓦斯(Clement Montois)

我在类似函数的化简器中遇到一些类型错误(我的目标是能够使用状态中的formData生成表单)。

但是由于我的reducer函数中存在此错误,我在如何键入表单属性上感到挣扎:

  Index signatures are incompatible.
          Types of property 'initialValues' are incompatible.
            Type 'string' is not assignable to type 'number'.ts(2322)

码:

type Attribute = StringAttribute | DecimalAttribute;

enum Type {
    DECIMAL = 'decimal',
    STRING = 'string',
}

interface StringAttribute {
    isErrored: boolean;
    isMissingData: boolean;
    initialValues: string;
    values: string;
    attributeDefinition: {
        type: Type.STRING;
        helperText: string;
        label: string;
    }
}

const isStringAttribute = (attribute: Attribute): attribute is StringAttribute => attribute.attributeDefinition.type === Type.STRING;

interface DecimalAttribute {
    isErrored: boolean;
    isMissingData: boolean;
    initialValues: number;
    values: number;
    attributeDefinition: {
        type: Type.DECIMAL;
        helperText: string;
        label: string;
    }
}

const isDecimalAttribute = (attribute: Attribute): attribute is DecimalAttribute => attribute.attributeDefinition.type === Type.DECIMAL;

type Action =
| { type: 'updateStringValue', attributeIdentifier: string; value: string }
| { type: 'updateDecimalValue', attributeIdentifier: string; value: number }

interface State {
    formData: Record<string, Attribute>
}

const reducer = (state: State, action: Action): State => {
    switch (action.type) {
        case 'updateDecimalValue': {
            const attribute = state.formData[action.attributeIdentifier];
            if (isDecimalAttribute(attribute)) {
                return {
                    ...state,
                    formData: {
                        ...state.formData,
                        [action.attributeIdentifier]: {
                            ...state.formData[action.attributeIdentifier],
                            values: action.value
                        }
                    }
                }
            }

            return state;
        }
    }
}

const initialState: State = {
    formData: {
        stringAttributeIdentifier: {
            isErrored: false,
            isMissingData: false,
            values: 'String data',
            initialValues: 'String data',
            attributeDefinition: {
                type: Type.STRING,
                helperText: 'Some Helper Text',
                label: 'My Attribute Label'
            }
        },
        decimalAttributeIdentifier: {
            isErrored: false,
            isMissingData: false,
            values: 13.32,
            initialValues: 13.32,
            attributeDefinition: {
                type: Type.DECIMAL,
                helperText: 'Some Helper Text',
                label: 'My Attribute Label'
            }
        },
    }
}
托比亚斯弗里德

TypeScript不能使您从化简器的每个分支返回的内容具有适合您要执行的操作的形状。您可以提供帮助:

编辑编辑:您必须已经使用attributes通过类型防护的东西():

const reducer = (state: State, action: Action): State => {
  switch (action.type) {
    case 'updateDecimalValue': {
      const attribute = state.formData[action.attributeIdentifier];
      if (isDecimalAttribute(attribute)) {
        return {
          ...state,
          formData: {
            ...state.formData,
            [action.attributeIdentifier]: {
              ...attribute,     // <---- THIS THING HAS PASSED THE TYPE GUARD
              values: action.value
            }
          }
        }
      }

      return state;
    }
    case "updateStringValue": {
      const attribute = state.formData[action.attributeIdentifier];
      if (isStringAttribute(attribute)) {
        return {
          ...state,
          formData: {
            ...state.formData,
            [action.attributeIdentifier]: {
              ...state.formData[action.attributeIdentifier], // <---- ERROR: THIS HASN'T
              values: action.value
            }    
          }
        }
      }
      return state;
    }
    default: throw new Error();
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类常见问题

减速器中的打字稿类型问题

来自分类Dev

MR减速器中的输出HBase增量

来自分类Dev

Redux:区分减速器中的对象

来自分类Dev

了解以下减速器中的扩散和破坏用途

来自分类Dev

在redux中是否可以嵌套化径减速器?

来自分类Dev

反应在父级减速器与子级减速器中完成的Redux逻辑

来自分类Dev

减速器中CRUD的“ U”(更新)部分的问题

来自分类Dev

在减速器中删除动态属性

来自分类Dev

打字稿减速机问题

来自分类Dev

减速器未在Redux中接收动作

来自分类Dev

打字稿中可调用类型关系的问题

来自分类Dev

在Redux中嵌套/组合合并的减速器?

来自分类Dev

打字稿React-Redux-动作未达到减速器

来自分类Dev

redux减速器类型“从不”

来自分类Dev

减速器中Redux有效负载的问题

来自分类Dev

在Typescripted Redux中,如何找到根减速器的类型?

来自分类Dev

如何简洁地实现减速器功能的类型推断?

来自分类Dev

如何修复减速器类型定义?(TS2345)

来自分类Dev

如何在Redux打字稿中使用多有效载荷接口进行减速器动作?

来自分类Dev

减速器异常

来自分类Dev

减速器未在hadoop中执行

来自分类Dev

清理未在减速器中运行

来自分类Dev

redux 减速器流量不推断类型

来自分类Dev

Spark减速器和求和结果问题

来自分类Dev

在减速器中设置状态会产生类型错误

来自分类Dev

在 react-native 中从另一个减速器更新减速器

来自分类Dev

接受减速器的函数的流类型不匹配

来自分类Dev

无法在组合减速器中使用减速器

来自分类Dev

redux 减速器中的打字稿