easy-peasy: Can't use index signature types in model with generics
Related issue: https://github.com/ctrlplusb/easy-peasy/issues/117
I’m using "easy-peasy": "2.3.0" and "typescript": "3.3.4000".
I have the following store, which produces a TS “error”:
interface IMyStoreModel<T extends IMyInterface> {
myMap: {
[id: string]: T;
};
values: Select<IMyStoreModel<T>, Array<T>>;
}
const generateModel = <T extends IMyInterface>() => {
const myStore: IMyStoreModel<T> = {
myMap: {},
values: select(state =>
Object.keys(state.map)
// 👇 error
.map(key => state.myMap[key])),
}
return myStore;
}
If I define IMyStoreModel without using generic types, the error disappears:
// works
interface IMyStoreModel {
myMap: {
[id: string]: IMyInterface;
};
values: Select<IMyStoreModel, Array<IMyInterface>>;
}
I could also cast state.myMap to its correct type - but that gets quite ugly.
Here is the exact error message produced:
(property) myMap: Compact<{ [K in (T extends Select<any, any> | Reducer<any, any> ? string : never) | (T extends Select<any, any> | Reducer<any, any> ? number : never) | Exclude<Exclude<…>, (T extends Select<…> | Reducer<…> ? string : never) | (T extends Select<…> | Reducer<…> ? number : never)> | Exclude<…>]: { [P in keyof Compact<…>]: Compact<…>[P]; }[K]; } & Compact<…>> Element implicitly has an ‘any’ type because type ‘Compact<{ [K in (T extends Select<any, any> | Reducer<any, any> ? string : never) | (T extends Select<any, any> | Reducer<any, any> ? number : never) | Exclude<Exclude<…>, (T extends Select<…> | Reducer<…> ? string : never) | (T extends Select<…> | Reducer<…> ? number : neve…’ has no index signature.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 24 (11 by maintainers)
Also tried upgrading TS to
3.5.1- but the issue is still present there 😢It’s also compatible with
typescript@3.3.4000FYIalso
easy-peasy@2.3.1-alpha.3worked like a charm. Seems like there was some issues witheasy-peasy@2.3.1-alpha.2- even got errors withuseStorehooks. All kinks gone after upgrading to alpha 3 👍It seems to be working, but there is something wierd with selectors - it does not seem to be part of the
stateobject? 🤔Updated testcase for you:
Error:
I’m guessing selectors is supposed to be part of the first
Compact-thingy?@jmyrland - 3rd times a charm.
Testing ATM - it seems that accessing the state is working, but setting the state is not. (TS 3.3.4000)
I have updated your test case to account for this as well:
Error message:
No worries, TBH I have been sticking to
3.3.4000as>=3.4have introduced performance regressions with definitions of libraries such as styled-components. 😃Sweet! Installed the alpha just now - and the error is gone! 👌 And I don’t have to type cast results with this change! 🎉
The current build of my project is currently unstable (failing tests due to breaking changes) - so I don’t know for certain if this change has any side effects - but as far as I can tell, it works as it should!
Wow, did not expect a resolution this quickly! Well done! 😁
I will give the alpha a shot and let you know how it works out in my case.
Ah, as I suspected. I have seen this when I’ve modeled my stores with class instances. This might be a good thing to document in relation to “how to model stores” and advice to use serializable types 👍