Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.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 为数组创建自定义类型_Typescript - Fatal编程技术网

Typescript 为数组创建自定义类型

Typescript 为数组创建自定义类型,typescript,Typescript,我正在将Typescript与Redux一起使用,并希望为初始状态设置一个类型 我看到的示例假设初始状态为键值对,如: const INITIAL_STATE: State = { id: '1', firstName: 'Michael', lastName: 'Black', } 其中类型定义为 export interface State { id: string firstName: string lastName: string } cons

我正在将Typescript与Redux一起使用,并希望为初始状态设置一个类型

我看到的示例假设初始状态为键值对,如:

const INITIAL_STATE: State = {
    id: '1',
    firstName: 'Michael',
    lastName: 'Black',
  }
其中类型定义为

export interface State {
  id: string
  firstName: string
  lastName: string
}
const INITIAL_STATE: State = [
  {
    id: '1',
    firstName: 'Michael',
    lastName: 'Black',
  },
  {
    id: '2',
    firstName: 'Tony',
    lastName: 'Montana',
  }
]
如果类型定义为

export interface State {
  id: string
  firstName: string
  lastName: string
}
const INITIAL_STATE: State = [
  {
    id: '1',
    firstName: 'Michael',
    lastName: 'Black',
  },
  {
    id: '2',
    firstName: 'Tony',
    lastName: 'Montana',
  }
]
类型定义看起来如何?我确实试着寻找答案,因为它看起来应该很简单,但找不到任何东西

编辑:

我想我能做到

const INITIAL_STATE: Array<State> = [
  {
    id: '1',
    firstName: 'Michael',
    lastName: 'Black',
  },
  {
    id: '2',
    firstName: 'Tony',
    lastName: 'Montana',
  }
]
const初始状态:数组=[
{
id:'1',
名字:“迈克尔”,
姓氏:“黑色”,
},
{
id:'2',
名字:“托尼”,
姓:“蒙大拿”,
}
]
但是如果我想要一个新的定义,CustomerState呢

差不多

export interface CustomerState Array<State>
导出接口CustomerState数组
这当然是一个语法错误

导出接口CustomerState数组

我会的

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',
  }
]

Array
State[]
?已尝试..'export interface CustomerState Array'我想我知道问题所在。请参阅更新