正在加载视频...

视频加载失败

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 次观看 • 3 年前 •via X (Twitter)

11 条评论

Ozzy 的头像
Ozzy3 年前

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

Wes Bos 的头像
Wes Bos3 年前

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 的头像
Arnab Chatterjee3 年前

I explored it today only

Jigal Sanders 的头像
Jigal Sanders3 年前

Hell this is typescript and I actually understood it.

Ricardo Busquet 的头像
Ricardo Busquet3 年前

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 的头像
Wes Bos3 年前

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 的头像
Simon Taylor3 年前

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

JavascriptEnthu(Shakil) 的头像
JavascriptEnthu(Shakil)3 年前

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

Sock, the dev 🧦 的头像
Sock, the dev 🧦3 年前

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 的头像
Ebn Sina3 年前

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

Wes Bos 的头像
Wes Bos3 年前

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

相关视频