0

I've come to join the typescript wagon and i wantede to keep using this immutability-helper lib when im updating my redux store, but for some reason now i get this error when im trying to execute a update?:

[ts] Argument of type '{ flags: { hideChat: { $set: boolean; }; }; }' is not assignable to parameter of type 'Spec'. Object literal may only specify known properties, and 'flags' does not exist in type 'Spec'.

export interface GlobalStateInit {
  flags: {
    hideChat: boolean
  }
}

const initialState: GlobalStateInit = {
  flags: {
    hideChat: false
  }
}

const reducer: Reducer<GlobalStateInit, GlobalAction> = (state = initialState, action) => {
  switch (action.type) {
    case getType(actions.toggleChat):
      return update(state, {
        flags: {
          hideChat: { $set: !state.flags.hideChat }
        }
      })
    default:
      return state
  }
}

export { reducer as GlobalReducer }

I was asuming this should be trivial and should just work out of the box, my jest test running in the background can figure this out but the VScode TS Linter gets a bit angry.

Not sure if this is a bug or if its just VScode that messes up.

Nopzen
  • 566
  • 2
  • 6
  • 19

1 Answers1

0

This is a deliberate compiler feature. Here's a short version of the same problem:

interface Example {
    name: string,
    val: number
}

const example: Example = {
    name: 'Fenton',
    val: 1,
    more: 'example'
}

The more property isn't part of the Example interface, so the compiler is going to warn you that you might have made a mistake. For example, if there is an optional member that you typed wrong, it will catch that for you:

interface Example {
    name: string,
    val: number,
    side?: string
}

const example: Example = {
    name: 'Fenton',
    val: 1,
    sdie: 'Left' // Oops, I meant "side"
}

In cases where you think you know better than the compiler, you can tell it that you're in charge:

interface Example {
    name: string,
    val: number
}

const example = {
    name: 'Fenton',
    val: 1,
    more: 'example'
} as Example

This allows additional members, while still ensuring you don't make a mistake such as:

// Lots of errors!
const example = {
    more: 'example'
} as Example
Fenton
  • 241,084
  • 71
  • 387
  • 401