Skip to content

Typescript: Extracting type of value from a types.map(ModelType) in a generic function drops actions and views from returned type #2153

Answered by thegedge
evelant asked this question in General
Discussion options

You must be logged in to vote

So the major issue here is that IMSTMap<IT extends IAnyType> is almost a Map<string | number, IT["Type"]>, but not quite because of this one method:

set(key: string | number, value: ExtractCSTWithSTN<IT>): this

If we change value to IT["type"] you'll find TS infers the right thing for K and V. In its current form, I'm guessing V gets inferred as ExtractCSTWithSTN<IT> | IT["Type"] which is the same as ExtractCSTWithSTN<IT>.

That leads to the next problem: since this is a view, IT["CreationType"] obviously doesn't have that property (only the props!).

That sums up the problem. I'll think about if there's a solution or not, but I'm highly doubtful.

In the meantime, swap out Map<K, V> for It…

Replies: 9 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by coolsoftwaretyler
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help/PR welcome Help/Pull request from contributors to fix the issue is welcome Typescript Issue related to Typescript typings
4 participants
Converted from issue

This discussion was converted from issue #1775 on February 26, 2024 01:01.