#Extend Object Type When Walking Through The Parameters Of A Function

25 messages · Page 1 of 1 (latest)

inland crystal
#

Extend Object Type When Walking Through The Parameters Of A Function

bleak pine
#

I think you would have to reorganise your a function to automatically curry each method and call it as such
a("", c => {})(c => { /* context can have a different type here */ })(...)

#

If you have a lot of methods and this is on a hot path you might run in to performance issues with that though

inland crystal
#

@bleak pine that'd be no option for me

#

if there's no "ideal" solution, i'd be happy with a workaround too

bleak pine
#

Pretty sure you'll have to settle for context simply being a Record<string, uknonw> then

inland crystal
#

anything - except the structure of the accepted parameters in the a function - could be changed

bleak pine
#

or specify all your keys beforehand and make it partial

inland crystal
bleak pine
#

not all

#

well

#

I bet you could make some inscrutible type that uses the length of the methods argument to add keys to the context type, but I don't think the result would even be ergonomic

inland crystal
#

do you have any other idea on your mind how i could pass data typesafely through those methods?

#

that's the goal behind all this code

bleak pine
#

Create each Context type separately and cast them in each function

#

also not typesafe

#

but I don't think you can be in this situation

inland crystal
#

that's messed up :/

bleak pine
#

You can still be typesafe with Record<string, unknown> and runtime checks

inland crystal
#

hey @bleak pine, me again

#

do you know if it would be possible to solve this if each of the functions would return the object which should be combined with the Context?

bleak pine
#

As far as I know, not without adjusting your a signature to take a set number of args

inland crystal
#

tell me what to change

#

or share a small code snippet please

bleak pine
#

I just don't think its possible/worth spending time figuring out. TS wasn't made to track fine grained changes to object properties like this