Thanks theo, I'll tell her she doesn't have to worry about my package being unsafe
@hsthast7183
10 ай бұрын
As long as it's inside a 'wrapper' 😂
@arcrad
10 ай бұрын
Now that's good DX
@huge_letters
10 ай бұрын
You don't need a helper - just make a function you return generic on the key :) (event: TKey, data: TRouter[TKey]['input'])=>null Also I think with the way you did stuff you can pass to event of type A data of event of type B. You get errors in your example because none of your events have data of this shape but you can still pass data from other events - even though you shouldn't be able to.
@justkant
10 ай бұрын
Nice solution on the returned function I think that he could also keep both `TRouter` and `TKey` in the first generic by adding a default generic type like `TKey extends keyof TRouter = keyof TRouter`, not sure if there is drawbacks to this tho
@huge_letters
10 ай бұрын
@@justkant no, the problem he was trying to solve is that if you specify a generic manually - you have to specify all of them, even if these generics have default values and TS can infer them. You still have to type them out - so Theo wanted to expose a createClient function which only has one generic parameter TRouter
@flyingmadpakke
10 ай бұрын
This. To add context to this general pattern, as the outer function (helper) doesn't use the TKey parameter at all, it doesn't need to enforce any constraints on it. Coupling the constraint at the latest possible point (In this case the first returned function) decreases coupling overall and makes the code more versatile while maintaining the same level of safety (its that same old loose coupling = more good). So, this is something to look out for with all functions returning functions, are the type constraints defined at the right level, or could they be moved inwards? Similarly, this can sometimes be used to refactor functions into functions that return functions or classes for this exact same reason :D
@ahmedAltariqi
10 ай бұрын
First example: wow I understand TypeScript 😮 Second example: WTH is that 🤯
@pau1phi11ips
10 ай бұрын
Yeah, it's mental
@muslim8622
10 ай бұрын
God ! This thumbnail is S-tier ! Don’t change it, pls
@AlvarLagerlof
10 ай бұрын
The Posthog example is great! Much easier than a bunch of functions with typed params.
@ViorelMocanu
10 ай бұрын
I resonate with your frustration, and I'll steal some of your ideas because I have a task I kept postponing to standardize the way we send Analytics events to multiple 3rd party APIs and a "middleware" like that needs a type-safe interface like this. Great video, thank you!
@JR-lo2ei
10 ай бұрын
I haven't had the benefit of code review, being a longtime startup founder. Videos like this make me feel like I'm getting the code-review experience without any of the finger-pointing.
@beck4715
10 ай бұрын
One thing I tried to do (but didn't end up having time for) was doing something similar for our i18n strings. The goal was to use EN as the source of truth and through TS magic, make sure we catch typos in key names and whatnot. Maybe there's something I'm missing here but it seemed neat and very doable with TS
@soverain
10 ай бұрын
Good ol' wrapper never disappoint.
@FIIRdesu
10 ай бұрын
Am using something similar on frontend side when calling our own backend procedures. The name of the procedure is the generic key, and the parameter and return types are inferred. But those types are all generated from the database instead of handwritten, so you can reliably get compiler errors when the backend does breaking changes.
@thadtayo
10 ай бұрын
Okay, I really thought this would be a video about how Zod has a massive vulnerability right now. Good video regardless!
@ChadElliott_THEtheChad
10 ай бұрын
What's the massive vulnerability?
@thadtayo
10 ай бұрын
Zod V3.22.2 ReDoS@@ChadElliott_THEtheChad
@Guilherme-qk9so
10 ай бұрын
I thought you meant "unsafe" as in security, not type safety
@SXsoft99
10 ай бұрын
Wrapping a specific existing package logic in project specific logic to enforce certain rules. I have to wonder how many newbies look at this and get their mind blown
@yiannis_p
10 ай бұрын
Damn I always know a Theo thumbnail is gonna kick ass but hell this one is just too good 👌
@ChadElliott_THEtheChad
10 ай бұрын
Curious why you created a helper function vs extending the module declaration. Obviously it's more difficult but you dont need to add additional runtime code to simply add type safety. Not to say i haven't done this myself 😅
@oussama40612
10 ай бұрын
I too expected module augmentation instead of this wrapper function...
@eshanchatterjee1250
10 ай бұрын
What do I do if my workplace and coworkers don't use proper types at all. I literally have a ts file that has around 1.5k lines of code that is purely types no logic just type definitions, that's how I get any type hints Edit I just don't understand why people refuse to use types
@flyingmadpakke
10 ай бұрын
You need to ease them into the addiction, you can't just give them pure cocaine from the start. You start by suggesting them build-in type mappings/predicates, stuff like Partial, Required, ReadOnly and NonNullable are good starting points. Then you can show them how to create their own type predicates, mappings etc. At this point, you should start seeing their addiction feed itself. Now they are ready for the heavy hitters, filtered mappings, recursive types, type set theory etc. Once they write more meta code than code, and can prove that "any" is actually the paradoxical aggregation of the set of everything and nothing, only used by heathens, then you will know they have reached the final stages of addiction :)
@skeleton_craftGaming
10 ай бұрын
I consume external dependencies for breakfast. That's why I write my own code [because All my external dependencies are well in my stomach ; so I can't use them] though you think this is bad in flipping type script... Try correctly using a C api in C++
@jordihc
10 ай бұрын
My guy. I was literally trying to add this a few weeks ago to inngest, but gave up. Certified boss
@lighty262
10 ай бұрын
You define unsafe packages by data types? 🤨
@andymutale368
10 ай бұрын
Ah just what I've been struggling with tgis past week
@EraYaN
10 ай бұрын
Why not have the event types be methods on a class or functions? Is there some reason that can’t work?
@henzyd
10 ай бұрын
The editor looks cute
@karsongrady
10 ай бұрын
if (package === unsafe) { wrapit() } ~theo
@davidsiewert8649
10 ай бұрын
Where are the links to the source code?
@samuelgunter
10 ай бұрын
The only way to stay 100% safe is to abstain from using all packages
@armaandhanji7151
10 ай бұрын
Thanks for the great content Theo!
@joshix833
10 ай бұрын
Why are calling code with any type safe?
@Cruzylife
10 ай бұрын
Why the 1am drop
@avid459
10 ай бұрын
I am surprised there isn't a package to deal with unsafe packages😂
@yourlow
10 ай бұрын
I am now type pilled
@abc123evoturbobonker
10 ай бұрын
I dunno, starting to sound a bit like JDSL..
@keith-is-mad
10 ай бұрын
5:25 - people shortening the "generic parameter" to just "generic" always triggers me internally. it's like calling "a function parameter" - "a function".
@Igstefano
10 ай бұрын
Username checks out
@arashitempesta
10 ай бұрын
me barely understanding how to use basic generics for reusing components watching this vid 😅
@daleryanaldover6545
10 ай бұрын
I do similar thing but in JSDoc
@kearfy
10 ай бұрын
pnpm rm …
@LGREC15
10 ай бұрын
LMAO "POST HOG"
@Its-InderjeetSinghGill
10 ай бұрын
Nice thumbnail though 😂😂😂
@user-dj6or6sz4u
10 ай бұрын
im so stpd
@tanishqmanuja
10 ай бұрын
Unsafe meant Not nicely typed packages 😂
@steav677
10 ай бұрын
typescript's inferring mechanisms are some works and others don't. That's fucking annoying. I understand why a handful ppl hates typescript. typescript is not a perfect type system yet.
@steav677
10 ай бұрын
but I LOVE typescript though
@dehrk9024
29 күн бұрын
does he have adhd?
@theLowestPointInMyLife
10 ай бұрын
That's some of the worst code I've seen in a long, long time. It's just typescript masturbation at that point.
Пікірлер: 57