代码之家  ›  专栏  ›  技术社区  ›  Aessandro

打字机问题

  •  1
  • Aessandro  · 技术社区  · 4 年前

    我有以下几点:

    ...
    
    type RepairsState = {
      data: Property[] /* Property is an object coming from another file */
    }
    
    type RepairsPropertyLoadAction = {
      type: typeof REPAIRS_PROPERTY_LOAD
      payload: { models: Property[] } /* the payload will return an object that has a models property of an array of objects that match my property type */
    }
    
    /* Reducer */
    export const initialState: RepairsState = {
      data: [
        {
          id: '',
          jobNo: '',
          trade: '', 
          priority: '',
          status: '',
          raisedDate: new Date(),
          appointmentDate: new Date(), 
          completedDate: new Date(),
          description: ''
        }
      ]
    }
    
    export default function reducer(state = initialState, action: RepairsPropertyLoadAction): RepairsState {
      switch (action.type) {
        case REPAIRS_PROPERTY_LOAD:
          console.log(action.payload)
          return {
            ...state,
            data: action.payload
          }
        default:
          return state
      }
    }
    
    export const getRepairsProperty = (state: AppState) => state.repairs.data
    
    ...
    

    Property 课程:

    export default class Property {
      id: string = ''
      jobNo: string = ''
      trade: string = ''
      priority: string = ''
      status: string = ''
      raisedDate: Date = new Date()
      appointmentDate: Date = new Date()
      completedDate: Date = new Date()
      description: string = ''
    }
    

    然而,我得到了以下错误:

    Type '{ models: Property[]; }' is missing the following properties from type 'Property[]': length, pop, push, concat, and 28 more. TS2740

    enter image description here

    2 回复  |  直到 4 年前
        1
  •  4
  •   Alexander    4 年前

    该操作返回一个 {models:Property[]} 对象,但状态为 数据:财产[]

    return {
       ...state,
       data: action.payload.model
    }
    
        2
  •  1
  •   wentjun    4 年前

    你错过了 models 属性,定义为 RepairsPropertyLoadAction .减速机应返回以下内容:

    return {
       ...state,
       data: action.payload.models,
    }