1

I am using Typescript with Redux and want to put a type for initial state.

Examples I have seen assume initial state is key value pairs like:

const INITIAL_STATE: State = {
    id: '1',
    firstName: 'Michael',
    lastName: 'Black',
  }

where the type is defined as

export interface State {
  id: string
  firstName: string
  lastName: string
}

How about if the type is defined as

const INITIAL_STATE: State = [
  {
    id: '1',
    firstName: 'Michael',
    lastName: 'Black',
  },
  {
    id: '2',
    firstName: 'Tony',
    lastName: 'Montana',
  }
]

How would the type definition look? I did try look for answer as it looks like it should be simple to do but couldn't find anything...

Edit:

I guess I could do

const INITIAL_STATE: Array<State> = [
  {
    id: '1',
    firstName: 'Michael',
    lastName: 'Black',
  },
  {
    id: '2',
    firstName: 'Tony',
    lastName: 'Montana',
  }
]

But how about if I wanted a new definition, CustomerState?

Something like

export interface CustomerState Array<State>

which is a syntax error of course.

3
  • Um Array<State> or State [] ? Commented May 7, 2019 at 1:10
  • Tried..'export interface CustomerState Array<State>' Commented May 7, 2019 at 1:11
  • i think i know the problem. see update Commented May 7, 2019 at 1:13

1 Answer 1

2

export interface CustomerState Array

I would

export interface Person {
  id: string
  firstName: string
  lastName: string
}
export type State = Person[];
const INITIAL_STATE: State = [
  {
    id: '1',
    firstName: 'Michael',
    lastName: 'Black',
  },
  {
    id: '2',
    firstName: 'Tony',
    lastName: 'Montana',
  }
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.