Video wird geladen...

Video konnte nicht geladen werden

Zur Startseite

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 Aufrufe • vor 3 Jahren •via X (Twitter)

11 Kommentare

Profilbild von Ozzy
Ozzyvor 3 Jahren

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

Profilbild von Wes Bos
Wes Bosvor 3 Jahren

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

Profilbild von Arnab Chatterjee
Arnab Chatterjeevor 3 Jahren

I explored it today only

Profilbild von Jigal Sanders
Jigal Sandersvor 3 Jahren

Hell this is typescript and I actually understood it.

Profilbild von Ricardo Busquet
Ricardo Busquetvor 3 Jahren

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.

Profilbild von Wes Bos
Wes Bosvor 3 Jahren

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

Profilbild von Simon Taylor
Simon Taylorvor 2 Jahren

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

Profilbild von JavascriptEnthu(Shakil)
JavascriptEnthu(Shakil)vor 2 Jahren

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

Profilbild von Sock, the dev 🧦
Sock, the dev 🧦vor 3 Jahren

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.

Profilbild von Ebn Sina
Ebn Sinavor 2 Jahren

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

Profilbild von Wes Bos
Wes Bosvor 2 Jahren

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

Ähnliche Videos