Typescript 为数组创建自定义类型
我正在将Typescript与Redux一起使用,并希望为初始状态设置一个类型 我看到的示例假设初始状态为键值对,如: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
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'我想我知道问题所在。请参阅更新