Loading video...

Video Failed to Load

Go Home

Did you know TypeScript <Generics> don't always need to be explicitly set? You can infer their types by the type of data being passed! Super powerful approach when writing library or util code.

57,847 views • 2 years ago •via X (Twitter)

11 Comments

Ozzy's profile picture
Ozzy2 years ago

Woah woah woah. What is this type hunting you’ve got there!?

Wes Bos's profile picture
Wes Bos2 years ago

It's called InlayHints - takes a bit to dialed in as there are lots of options, but it's been in VS code for a bit now. Your theme also needs to support it

Arnab Chatterjee's profile picture
Arnab Chatterjee2 years ago

I explored it today only

Jigal Sanders's profile picture
Jigal Sanders2 years ago

Hell this is typescript and I actually understood it.

Ricardo Busquet's profile picture
Ricardo Busquet2 years ago

that's the main usage for generics and I do feel the TS community doesn't use it as we should. The point of generics is to allow for writing code that works for any types while adding constraints on the inputs and outputs so the interfaces are always safe.

Wes Bos's profile picture
Wes Bos2 years ago

Agreed, but the point isn't to let them always be inferred - not sure if that is what you meant. It's often handy to specify the type explicitly and then be able to safely type the arguments of a function

Simon Taylor's profile picture
Simon Taylor2 years ago

Why we need your TS course 🥲 are you planning to release anytime soon Wes?

JavascriptEnthu(Shakil)'s profile picture
JavascriptEnthu(Shakil)2 years ago

Typescript is much needed for me. And I am on my way

Sock, the dev 🧦's profile picture
Sock, the dev 🧦2 years ago

A little gotcha to be aware of that bit me, is if you have two or more generic parameters; function foo<ParamOne, ParamTwo = unknown>( one: ParamOne, two: ParamTwo ) If you pass in only the first generic arg (foo<String>), the rest won't be inferred. It's all or nothing.

Ebn Sina's profile picture
Ebn Sina2 years ago

Will be ever your typescript course will be launch? It's been so long!

Wes Bos's profile picture
Wes Bos2 years ago

It has - I'm almost done .Should have some updates on it soon

Related Videos