How I Deal With Unsafe Packages

  Рет қаралды 23,629

Theo - t3․gg

Theo - t3․gg

Күн бұрын

Obviously I love TypeScript. Sadly, not everyone does, and even fewer get typesafety correct when building packages with TypeScript.
I hope my demo of adding better typesafe wrappers to both PostHog and Inngest is helpful to y'all!
ALL MY VIDEOS ARE POSTED EARLY ON PATREON / t3dotgg
Everything else (Twitch, Twitter, Discord & my blog): t3.gg/links
S/O Ph4se0n3 for the awesome edit 🙏

Пікірлер: 56
@readywhen
@readywhen 8 ай бұрын
Thanks theo, I'll tell her she doesn't have to worry about my package being unsafe
@hsthast7183
@hsthast7183 8 ай бұрын
As long as it's inside a 'wrapper' 😂
@arcrad
@arcrad 8 ай бұрын
Now that's good DX
@huge_letters
@huge_letters 8 ай бұрын
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
@justkant 8 ай бұрын
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
@huge_letters 8 ай бұрын
@@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
@flyingmadpakke 8 ай бұрын
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
@muslim8622
@muslim8622 8 ай бұрын
God ! This thumbnail is S-tier ! Don’t change it, pls
@ahmedAltariqi
@ahmedAltariqi 8 ай бұрын
First example: wow I understand TypeScript 😮 Second example: WTH is that 🤯
@pau1phi11ips
@pau1phi11ips 8 ай бұрын
Yeah, it's mental
@AlvarLagerlof
@AlvarLagerlof 7 ай бұрын
The Posthog example is great! Much easier than a bunch of functions with typed params.
@JR-lo2ei
@JR-lo2ei 8 ай бұрын
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.
@ViorelMocanu
@ViorelMocanu 8 ай бұрын
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!
@beck4715
@beck4715 8 ай бұрын
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
@FIIRdesu
@FIIRdesu 8 ай бұрын
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.
@yiannis_p
@yiannis_p 8 ай бұрын
Damn I always know a Theo thumbnail is gonna kick ass but hell this one is just too good 👌
@soverain
@soverain 8 ай бұрын
Good ol' wrapper never disappoint.
@Guilherme-qk9so
@Guilherme-qk9so 8 ай бұрын
I thought you meant "unsafe" as in security, not type safety
@thadtayo
@thadtayo 8 ай бұрын
Okay, I really thought this would be a video about how Zod has a massive vulnerability right now. Good video regardless!
@ChadElliott_THEtheChad
@ChadElliott_THEtheChad 8 ай бұрын
What's the massive vulnerability?
@thadtayo
@thadtayo 7 ай бұрын
Zod V3.22.2 ReDoS@@ChadElliott_THEtheChad
@ChadElliott_THEtheChad
@ChadElliott_THEtheChad 8 ай бұрын
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
@oussama40612 8 ай бұрын
I too expected module augmentation instead of this wrapper function...
@armaandhanji7151
@armaandhanji7151 8 ай бұрын
Thanks for the great content Theo!
@EraYaN
@EraYaN 8 ай бұрын
Why not have the event types be methods on a class or functions? Is there some reason that can’t work?
@jordihc
@jordihc 8 ай бұрын
My guy. I was literally trying to add this a few weeks ago to inngest, but gave up. Certified boss
@eshanchatterjee1250
@eshanchatterjee1250 8 ай бұрын
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
@flyingmadpakke 8 ай бұрын
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 :)
@henzyd
@henzyd 8 ай бұрын
The editor looks cute
@SXsoft99
@SXsoft99 8 ай бұрын
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
@andymutale368
@andymutale368 8 ай бұрын
Ah just what I've been struggling with tgis past week
@davidsiewert8649
@davidsiewert8649 8 ай бұрын
Where are the links to the source code?
@skeleton_craftGaming
@skeleton_craftGaming 8 ай бұрын
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++
@lighty262
@lighty262 8 ай бұрын
You define unsafe packages by data types? 🤨
@Cruzylife
@Cruzylife 8 ай бұрын
Why the 1am drop
@yourlow
@yourlow 8 ай бұрын
I am now type pilled
@avid459
@avid459 8 ай бұрын
I am surprised there isn't a package to deal with unsafe packages😂
@karsongrady
@karsongrady 8 ай бұрын
if (package === unsafe) { wrapit() } ~theo
@Its-InderjeetSinghGill
@Its-InderjeetSinghGill 8 ай бұрын
Nice thumbnail though 😂😂😂
@joshix833
@joshix833 8 ай бұрын
Why are calling code with any type safe?
@samuelgunter
@samuelgunter 8 ай бұрын
The only way to stay 100% safe is to abstain from using all packages
@LGREC15
@LGREC15 8 ай бұрын
LMAO "POST HOG"
@arashitempesta
@arashitempesta 8 ай бұрын
me barely understanding how to use basic generics for reusing components watching this vid 😅
@abc123evoturbobonker
@abc123evoturbobonker 8 ай бұрын
I dunno, starting to sound a bit like JDSL..
@keith-is-mad
@keith-is-mad 8 ай бұрын
5:25 - people shortening the "generic parameter" to just "generic" always triggers me internally. it's like calling "a function parameter" - "a function".
@Igstefano
@Igstefano 7 ай бұрын
Username checks out
@daleryanaldover6545
@daleryanaldover6545 8 ай бұрын
I do similar thing but in JSDoc
@user-dj6or6sz4u
@user-dj6or6sz4u 8 ай бұрын
im so stpd
@kearfy
@kearfy 8 ай бұрын
pnpm rm …
@tanishqmanuja
@tanishqmanuja 8 ай бұрын
Unsafe meant Not nicely typed packages 😂
@steav677
@steav677 8 ай бұрын
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
@steav677 8 ай бұрын
but I LOVE typescript though
@theLowestPointInMyLife
@theLowestPointInMyLife 8 ай бұрын
That's some of the worst code I've seen in a long, long time. It's just typescript masturbation at that point.
@siduck_og
@siduck_og 8 ай бұрын
ugh typescript!
React + Servers = Confusion
20:30
Theo - t3․gg
Рет қаралды 39 М.
The Coolest Thing I Ever Built
10:28
Theo - t3․gg
Рет қаралды 64 М.
Why? 😭 #shorts by Leisi Crazy
00:16
Leisi Crazy
Рет қаралды 47 МЛН
1 класс vs 11 класс (неаккуратность)
01:00
БЕРТ
Рет қаралды 4,7 МЛН
Would you like a delicious big mooncake? #shorts#Mooncake #China #Chinesefood
00:30
The State Of HTML
24:25
Theo - t3․gg
Рет қаралды 36 М.
Cool Tools I’ve Been Using Lately
23:11
Theo - t3․gg
Рет қаралды 87 М.
as const: the most underrated TypeScript feature
5:38
Matt Pocock
Рет қаралды 109 М.
Facebook Tried Tailwind, Then Built This Instead
28:18
Theo - t3․gg
Рет қаралды 125 М.
My Favorite State Manager Is...URLs?
6:29
Theo - t3․gg
Рет қаралды 68 М.
The Most Hyped JavaScript Of 2023
33:18
Theo - t3․gg
Рет қаралды 78 М.
The Truth About React Server Components
26:33
Theo - t3․gg
Рет қаралды 43 М.
How Did I Not Know This TypeScript Trick Earlier??!
9:11
Josh tried coding
Рет қаралды 201 М.
Building the Ultimate Nike App in React Native & Redux
3:33:55
notJust․dev
Рет қаралды 144 М.
iPhone 15 Pro vs Samsung s24🤣 #shorts
0:10
Tech Tonics
Рет қаралды 9 МЛН
3.5.A Solar Mobile 📱 Charger
0:39
Gaming zone
Рет қаралды 320 М.
Samsung or iPhone
0:19
rishton vines😇
Рет қаралды 8 МЛН
The power button can never be pressed!!
0:57
Maker Y
Рет қаралды 54 МЛН