0

https://codesandbox.io/s/otherusersmap-typescript-problem-gpkbhf?file=/src/index.ts

So my Redux reducer uses a bunch of TypeScript records to make fetching information faster. So, I created a general function for mapping an object with its id. However, I have this object that breaks the usual convention of the object being stored extending {id:number}. In this case, it's [string, Type extending {id:number}].

The function is generateStateMapFromArr, as you can see in the sandbox. For some reason TypeScript is saying that the returned object is just {id: number}, when it should be [string, Type<extends {id:number}]. It however works for the case when Type<extends {id:number}> is the input.

I figure the problem is with generateStateMapFromArr input type, but I don't know what exactly is the issue.

Also it's my first time using CodeSandbox.io, so let me know if there's an issue.

Brandon-Perry
  • 366
  • 4
  • 18
  • There's a lot going on in that code sandbox, would it be possible for you to reduce the problem to a smaller amount of code that is representative? – Souperman Nov 13 '22 at 14:24
  • @Souperman Oh I guess this had a bunch of extra files that weren't useful. I removed those, cleaned up the code a bit, and added comments. I don't think I could get anymore abstract without understanding the problem I have first. Let me know what you think. – Brandon-Perry Nov 14 '22 at 01:04
  • I think you just have some misaligned types which you can see more clearly if you set an explicit parameter when you call `generateStateMapFromArr`. If you say `getgenerateStateMapFromArr` you'll get a type error that points out that it expects `Type` to extend `{ id: number }` but that type is the tagged union. Perhaps, instead you should make your type `type OtherUsersMap = Record;` if it's going to extract the user data from the tagged union. – Souperman Nov 14 '22 at 08:57

0 Answers0