Home > Back-end >  Redux state interface: An interface can only extend an object type or intersection of object types w
Redux state interface: An interface can only extend an object type or intersection of object types w

Time:12-16

I’m trying to create a type definition for my component props:

interface ComponentProps<MappedStateProps = AppStateStateProps> extends MappedStateProps, OtherProps {
   // ... some other props here too
}

MappedStateProps can either be:

  • the full app state (if a MappedStateProps type argument is not given), or
  • a subset of the state (if MappedStateProps defined - but it won’t always be defined, so in that case we want the full state to be assumed - a.k.a AppStateStateProps)

The error I get is: An interface can only extend an object type or intersection of object types with statically known members.

How can I solve this?

Extra info:

Example usage:

const a: ComponentProps<{ foo: string }<-- only foo in our state props

or

const b: ComponentProps  <-- full state in state props

where

type AppStateStateProps = {
    aStr: string;
    aNum: number;
}

CodePudding user response:

interface does not allow you to extends an object with statically unknown members, but type does.

Please use type instead of interface:

type ComponentProps<MappedStateProps = AppStateStateProps> = MappedStateProps & OtherProps
  • Related