如何输入规范化状态树的分支?
How to type branches of normalized state tree?
我有一个关于在 ngrx
库中输入规范化状态树的问题。例如:
const initialState: State = {
dependencies: {
1: {
id: 1,
name: "dependency1",
type: "basic"
},
2: {
id: 2,
name: "dependency2",
type: "complex"
},
3: {
id: 3,
name: "dependency3",
type: "basic"
}
},
dependencyIds: [1, 2, 3]
}
好的,那么我们可以在State
接口中将dependencyIds
声明为number[]
,这样就很清楚了。 但问题是:有没有办法将 dependecies
键入为对象,在每个参数中都有一个 Dependency
对象?
像:
export interface State {
dependencies: any, <--- is there a {Dependency} instead of 'any' type, or sth?
dependencyIds: number[]
}
是的,这是可能的。您可以使用 dependencies: { [key: number]: {...}
interface State {
dependencies: { [key: number]: {
id: number,
name: string,
type: "basic" | "complex"
} }
dependencyIds: number[]
}
或后续接口:
interface State {
dependencies: { [key: number]: Dependency},
dependencyIds: number[];
}
interface Dependency {
id: number,
name: string,
type: "basic" | "complex"
}
我有一个关于在 ngrx
库中输入规范化状态树的问题。例如:
const initialState: State = {
dependencies: {
1: {
id: 1,
name: "dependency1",
type: "basic"
},
2: {
id: 2,
name: "dependency2",
type: "complex"
},
3: {
id: 3,
name: "dependency3",
type: "basic"
}
},
dependencyIds: [1, 2, 3]
}
好的,那么我们可以在State
接口中将dependencyIds
声明为number[]
,这样就很清楚了。 但问题是:有没有办法将 dependecies
键入为对象,在每个参数中都有一个 Dependency
对象?
像:
export interface State {
dependencies: any, <--- is there a {Dependency} instead of 'any' type, or sth?
dependencyIds: number[]
}
是的,这是可能的。您可以使用 dependencies: { [key: number]: {...}
interface State {
dependencies: { [key: number]: {
id: number,
name: string,
type: "basic" | "complex"
} }
dependencyIds: number[]
}
或后续接口:
interface State {
dependencies: { [key: number]: Dependency},
dependencyIds: number[];
}
interface Dependency {
id: number,
name: string,
type: "basic" | "complex"
}