Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 类reducer函数中的类型脚本类型问题_Typescript_Typescript Typings - Fatal编程技术网

Typescript 类reducer函数中的类型脚本类型问题

Typescript 类reducer函数中的类型脚本类型问题,typescript,typescript-typings,Typescript,Typescript Typings,我在一个类似于reducer的函数中遇到一些类型错误(我的目标是能够从我的状态使用formData生成表单) 但是,由于我的reducer函数中存在以下错误,我正在努力输入表单属性: Index signatures are incompatible. Types of property 'initialValues' are incompatible. Type 'string' is not assignable to type 'number

我在一个类似于reducer的函数中遇到一些类型错误(我的目标是能够从我的状态使用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;
枚举类型{
DECIMAL='DECIMAL',
字符串='STRING',
}
接口字符串属性{
isErrored:布尔值;
isMissingData:布尔值;
初始值:字符串;
值:字符串;
属性定义:{
类型:type.STRING;
helperText:字符串;
标签:字符串;
}
}
常量isStringAttribute=(属性:属性):属性为StringAttribute=>attribute.attributeDefinition.type==type.STRING;
接口DecimalAttribute{
isErrored:布尔值;
isMissingData:布尔值;
初始值:数字;
数值:数字;
属性定义:{
类型:type.DECIMAL;
helperText:字符串;
标签:字符串;
}
}
常量isDecimalAttribute=(属性:属性):属性为DecimalAttribute=>attribute.attributeDefinition.type==type.DECIMAL;
类型动作=
|{type:'updateStringValue',attributeIdentifier:string;value:string}
|{type:'updateDecimalValue',attributeIdentifier:string;value:number}
界面状态{
formData:记录
}
const reducer=(state:state,action:action):state=>{
开关(动作类型){
案例“UpdatedCIMalValue”:{
const attribute=state.formData[action.attributeIdentifier];
if(isDecimalAttribute(属性)){
返回{
……国家,
表格数据:{
…state.formData,
[action.attributeIdentifier]:{
…state.formData[action.attributeIdentifier],
价值观:action.value
}
}
}
}
返回状态;
}
}
}
常量initialState:状态={
表格数据:{
stringAttributeIdentifier:{
isErrored:错,
isMissingData:错误,
值:“字符串数据”,
initialValues:'字符串数据',
属性定义:{
type:type.STRING,
helperText:“某些帮助器文本”,
标签:“我的属性标签”
}
},
DecimalAttribute标识符:{
isErrored:错,
isMissingData:错误,
数值:13.32,
初始值:13.32,
属性定义:{
类型:type.DECIMAL,
helperText:“某些帮助器文本”,
标签:“我的属性标签”
}
},
}
}

TypeScript无法确保从减速器的每个分支返回的内容具有适合您要执行的操作的正确形状。您可以帮助它:

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

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();
  }
}
const reducer=(状态:状态,动作:动作):状态=>{
开关(动作类型){
案例“UpdatedCIMalValue”:{
const attribute=state.formData[action.attributeIdentifier];
if(isDecimalAttribute(属性)){
返回{
……国家,
表格数据:{
…state.formData,
[action.attributeIdentifier]:{

…attribute,//老实说,虽然您可能希望让
Attributes
成为
DecimalAttributes
StringAttributes
扩展的接口,但这样您就可以在不具体化联合类型的情况下获得多态性。好的,谢谢它确实起作用了,我认为检查属性类型(使用类型保护)拥有值的类型就足够了。我不知道我必须强制转换它!谢谢你的回答!哦,你实际上没有。只是更新了我的答案,但你只有typeguard
属性,而不是
state.formData[action.attributeIdentifier]
,所以在这里它仍然可以是任意一种属性类型。
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();
  }
}