从@ngrx/entity/Dictionary 获取错误类型
Getting wrong type from @ngrx/entity/Dictionary
我有一些基于 NGRX 的映射如下,其中之一正在返回:
Type 'NormalizedAppState[key]' does not satisfy the constraint 'EntityState<NormalizedAppState[key]["entities"][number]>'.
Type 'InstrumentState' is not assignable to type 'EntityState<NormalizedAppState[key]["entities"][number]>'.
Types of property 'entities' are incompatible.
Type 'Dictionary<IInstrument>' is not assignable to type 'Dictionary<NormalizedAppState[key]["entities"][number]>'.
Type 'IInstrument' is not assignable to type 'NormalizedAppState[key]["entities"] [number]'.
我的映射
//* Minimal NGRX *//
interface DictionaryNum<T> {
[id: number]: T | undefined;
}
class Dictionary<T> implements DictionaryNum<T> {
[id: string]: T | undefined;
// [id: number]: T | undefined; // this solves the problem
}
interface EntityState<T> {
ids: number[];
entities: Dictionary<T>;
}
//* *//
const featureNames = {
instrument: 'instrument',
} as const
interface EntityStateActions<
TState extends EntityState<TState['entities'][number]>
> {}
interface IInstrument {}
interface InstrumentState extends EntityState<IInstrument> {}
abstract class NormalizedAppState {
[featureNames.instrument]: InstrumentState;
}
错误
export type NormalizedFeatureActions = {
[key in keyof typeof featureNames]: EntityStateActions<
NormalizedAppState[key] --> The error happens here
>;
};
如果我将 Dictionary<T>
从
更改为 class,错误就会消失
class Dictionary<T> implements DictionaryNum<T> {
[id: string]: T | undefined;
}
至
class Dictionary<T> implements DictionaryNum<T> {
[id: number]: T | undefined;
}
但是我不想换第三方class。而且用自己的Dictionary也是有问题的,需要替换其他几种类型
我怎样才能摆脱这个错误?
我找到的解决方案是在我的接口中放置更多的参数,这样他们就不需要通过索引来访问类型了。
interface EntityActions<TEntity,TState extends EntityState<TEntity>>{}
abstract class AppState {
[featureNames.instrument]: InstrumentState;
}
export type FeatureActions = {
[featureNames.instrument]: EntityActions<IInstrument,InstrumentState>
};
我有一些基于 NGRX 的映射如下,其中之一正在返回:
Type 'NormalizedAppState[key]' does not satisfy the constraint 'EntityState<NormalizedAppState[key]["entities"][number]>'.
Type 'InstrumentState' is not assignable to type 'EntityState<NormalizedAppState[key]["entities"][number]>'.
Types of property 'entities' are incompatible.
Type 'Dictionary<IInstrument>' is not assignable to type 'Dictionary<NormalizedAppState[key]["entities"][number]>'.
Type 'IInstrument' is not assignable to type 'NormalizedAppState[key]["entities"] [number]'.
我的映射
//* Minimal NGRX *//
interface DictionaryNum<T> {
[id: number]: T | undefined;
}
class Dictionary<T> implements DictionaryNum<T> {
[id: string]: T | undefined;
// [id: number]: T | undefined; // this solves the problem
}
interface EntityState<T> {
ids: number[];
entities: Dictionary<T>;
}
//* *//
const featureNames = {
instrument: 'instrument',
} as const
interface EntityStateActions<
TState extends EntityState<TState['entities'][number]>
> {}
interface IInstrument {}
interface InstrumentState extends EntityState<IInstrument> {}
abstract class NormalizedAppState {
[featureNames.instrument]: InstrumentState;
}
错误
export type NormalizedFeatureActions = {
[key in keyof typeof featureNames]: EntityStateActions<
NormalizedAppState[key] --> The error happens here
>;
};
如果我将 Dictionary<T>
从
class Dictionary<T> implements DictionaryNum<T> {
[id: string]: T | undefined;
}
至
class Dictionary<T> implements DictionaryNum<T> {
[id: number]: T | undefined;
}
但是我不想换第三方class。而且用自己的Dictionary也是有问题的,需要替换其他几种类型
我怎样才能摆脱这个错误?
我找到的解决方案是在我的接口中放置更多的参数,这样他们就不需要通过索引来访问类型了。
interface EntityActions<TEntity,TState extends EntityState<TEntity>>{}
abstract class AppState {
[featureNames.instrument]: InstrumentState;
}
export type FeatureActions = {
[featureNames.instrument]: EntityActions<IInstrument,InstrumentState>
};