Mapped Types Not Inferring From Promise Correctly
TS mapped types not behaving as expected on promise inference
Desired DX
Package types
Currently have "unknown" coming through in the onUpload type. If I manually define the type on request (i.e.
onUpload: async (request: {metadata: {userId: string}})
), it will "backfill" correctly
Failing to get the type to infer off Promise<T> to the onUpload input111 Replies
Also this would attach the return type as a promise I think
Yeah expected the awaited
Yeah something's weird here
Seemed like such a simple case
Is this solving the problem in the right location?
Removing the generic input definition for a moment and replacing it with:
generates the correct type for metadata
it makes me thing that it needs to be solved at this line here, this doesn't work but is along the lines of what im trying
This is roughly how I planned on solving it before realizing I'm not good enough at TS and posting here
Is @trash_dev in this server? Cool he is hi trash pls help
I think you want a template type?
I'm not paid enough to know what this means
Went down a rabbit hole with
infer
for a bit and it was roughAssuming the key name is different when you map it (it seemed like that in your Tweet)
input
here has many keys
It's the first arg, second one is config (which i hate and will change eventually)Oh I misread it 🤦♂️
Can you give an example of how this helper is consumed (assuming inference is implemented correctly)?
Irrelevant to the prompt, the inference within the helper's arguments are bad
Yes I'm aware it's not working, I'm asking how a dependent type would look assuming it was working
Is it like tRPC and I just call something like
useRouter(router).someKey
?Like how FileRouter's type looks?
It exports a POST wrapper lol
so request.metadata should have the return type of middleware?
Yes
does this work?
oh no it wont if different keys return different types of response
im taking my gold star away <:sad_cowboy:632072121945817108>
lmao
This was how I ended up in the keyof hell
If I presume all the middleware is the same this is way easier
i'm glad im not a ts library writer lol
Right
I am suffering so most people don’t have to
I’m so mad I got zact right first try and not this one lol
I used to work on Redux Toolkit and it's in a different dimension of understanding TS than what I have now
Might change the syntax to be more tRPC procedure like but that’s 10x harder to implement and I’ll have to do a bunch of weird this bindings
FUCKING CHRIST CHATGPT* GOT IT FIRST TRY
we're all fucked
Not copilot
Chatgpt
Regardless
We're so fucked
No idea how this works
it makes sense tho
crazy
I mean, why does
onUpload
need its own generic parameter?im still trynna read through and understand it it feels wrong to me
UploadableInputParams<TValidRoutes[K]>;
That is being given:
So TMiddlewareResponse would be that dict object, which I have no idea how that gets to become the return type value of middleware since nothing is converting it to thatam i dumb or will it show error if you try to access its props?
im guessing there were some modifications to UploadThingServerHelper as well
No TS bot? 😢
The more I troubleshoot this the less I understand
so it's done? chatgpt got the right solution?
My guess is that we need some conditional type fuckery to get the compiler to apply the homomorphism properly
No, keying off the metadata breaks inference https://discord.com/channels/966627436387266600/1097033800019615827/1097053896108679219
I'll make a playground off it in a bit, hanging with friends rn
what's broken in this one?
I asked chatgpt more and it gave a better answer
Specifically this:
Annoying but I can add a branded type and live with it
why did you leave the builder pattern?
Keying off the metadata breaks inference https://discord.com/channels/966627436387266600/1097033800019615827/1097053896108679219
oh i see now xdd
i like this pattern
Are middleware and onUpload always required?
This was too hard to do, can I pay you to do it for me lol
you have a reference codebase you can snag from 🙂
That reference codebase is like 2 standard deviations away from my comprehension
You’re in our GitHub org already I’ll invite you
Early warning I’m also drunk with friends and on mobile rn lmao
You should get that balmer peak git plugin
There's a git plugin?
👀
GitHub
GitHub - noidontdig/gitdown: Don't commit when you're drunk
Don't commit when you're drunk. Contribute to noidontdig/gitdown development by creating an account on GitHub.
A college friend of mine had it in Ballmer mode so he could only commit while drunk enough
same question
on it
im close
going out - will come back later
got it @theo
i just woke up, this is the longest thread ever
did you reference the trpc middleware stuff @marminge
Oh yes 🙄
👀👀
No piping though so a lot simpler
guessing you couldn’t chain without builder pattern yeah?
AFAIK you can’t infer return type of a different object key as input to another - that’s where the builder comes in
Builder FTW
sounds about right
i bet you can but if it works it works
builder is the only GoF pattern you ever need 😉
you share the working code @marminge 🙂
When I get home
sweet, still in bed no rush
my non builder pattern way
i think that works? 🤷♂️
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
that looked liek it was working but it wasnt
oh i was gonna send my builder
hang on
click my link
ah yea it doesn't have the same problem
wizard
you need an identity fn thingy
idk real terms
yeye that's the difference
isnt it working in that one?
in the one that you linked
this is the gist of the builder:
nah it looks like it but when you try traversing the metadata object it doesn't work anymore
ah i see! thats.. dumb
yep...
the inlay hints are lying?
the identity function is prob the thing that fixes it then
yea
yeah you cant infer properly so you need another layer to get the second inference to metadata
or something.. idk i just type stuff in until it works
i think tanstack router does the same thing(uses fns to create its configs) need to look though
@trash_dev do you know why this isn't autocompleting?
are those sizes hardcoded or something?
one sec out atm
if i had to guess its cause you can have any number of
numbers
so it doesnt know when to suggest the suffixesmakes sense - but it doesn't autocomplete even after i type the G for example
to GB
its bugging me now though. id ask in pococks discord
it works if you give it a finit set of options
number is too broad it doesnt really know when its the end even if you have a G
so if theres a max limit you can make your on number validation
ahh
i guess 1 10 or 100 is good then
can i make it so these are autocompletable, but any number is acceptable?
yeah so you can make something that accounts for like 0 - {yourLimit}
heres a good example
https://twitter.com/lukemorales/status/1564424193877368832?s=20
Luke Morales (@lukemorales)
@t3dotgg @alexturpin_ @bedesqui BTW, the example goes from 1 all the way up to 99.
But you can use the principles and build the types until the number of chapters you need.
The snippet below goes from 1 to 53:
Twitter
this'll work
heck yeah
I'm surprised this doesn't fix it - I'd expect adding a : would be enough to define the split between number and SizeUnit letting autocomplete kick in
yeah i don’t think template literal type is smart enough or rather evaluates lazily shrug
Can u share your curated references to dive into builder pattern i need a refresh
He already mentioned GoF
Design Patterns: Elements of Reusable Object-Oriented Software
Design Patterns: Elements of Reusable Object-Oriented Software
probably worth actually saying gof means gang of four
ok this as eating me alive and it turns out it was a lot simpler
https://tsplay.dev/mL8dVm
cc @theo
if you dont put request: Request inference brakes LOL
updated code snippet to arrows pointing
if you remove that type annotation it breaks.......
lol
i made a simpler version of this and asked in matts discord.. and noticed that small fix... im gonna cry
but that seems .. kinda whack
What the actual fuck
I hate TS
that.. has to be a bug right?
You're the wizard not me
ive been nerd sniped so hard
If you need more fun
So cursed
me reading andarists replies