如何解决从@ 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 Dictionary<T> implements DictionaryNum<T> {
[id: string]: T | undefined;
}
到
class Dictionary<T> implements DictionaryNum<T> {
[id: number]: T | undefined;
}
但是我不想更改第三方类别。而且使用我自己的字典也有问题,因为有必要替换其他几种类型。
如何摆脱这个错误?
解决方法
我找到的解决方案是在我的接口中放入更多参数,这样他们就不需要通过索引访问类型。
interface EntityActions<TEntity,TState extends EntityState<TEntity>>{}
abstract class AppState {
[featureNames.instrument]: InstrumentState;
}
export type FeatureActions = {
[featureNames.instrument]: EntityActions<IInstrument,InstrumentState>
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。