4 ways to use the TypeScript infer keyword

preview_player
Показать описание
I've always been a little baffled by TypeScript's `infer` keyword? So I explored the ways you can use it, and came up with 4 variations! If you have other fun examples of how to use `infer`, let me know in the comments!

0:00 - Intro
0:32 - Generic Arguments
2:25 - Arrays
4:37 - Function Parameters and Return type
7:03 - Template Literals
9:53 - Outro
Рекомендации по теме
Комментарии
Автор

I've always never understood infer. But the examples you have shown is the best that made me understand finally.

stonecoldcold
Автор

That is the best explanation of infer ever.
It is not fair.
Why shitty stuff like "BECOME A FRONTEND DEVELOPER IN 5 SECONDS" is much more popular...

ДаниилСоболев-щщ
Автор

Two reasons to remember and share this channel with anybody:
1.content (on TS) second to none.
2.family name -Burgess--same as of Antony Burgess--"Clockwork Orange".

dimitargetsov
Автор

This was cool. I didn't know anything about infer. Thanks to your video and challenges, now I know. Keep up the good work!

the_proletariat
Автор

My implementation of FirstParam and SecondParam:
type FirstParameter<T extends (...args: any[]) => any> = T extends (firstParam: infer A, ...args: any[]) => any ? A: never;
type SecondParam<T extends (...args: any[]) => any> = T extends (firstParam: any, secondParam: infer A, ...args: any[]) => any ? A: never;

Also Jojo is everywhere LMAO

kyuubyemy
Автор

Great job! This is definitely the best video about infer on 2022!

serglohm
Автор

This was Epix.
Looking out for more.

vishwajeetraj
Автор

Wow! This has got to be the best video on the infer keyword I've ever seen. Keep up the good work Andrew! Liked and subbed 😀

vihanv
Автор

This was legitimately fun to code along with. Thank you!

AiguretDuren
Автор

Really nice demonstrations. Thanks a lot! It really made me understand the infer keyword better!

i.j.
Автор

This was the best explanation for infer. Cheers!

이지용-gw
Автор

Upped the production quality. Looks dope

devinorium
Автор

Dayum. This is very useful. Thanks for sharing.

johnyepthomi
Автор

Great explanation on this. Infer is so powerful

joshreynolds
Автор

What a great explanation, thank you so much!

УДжорджа
Автор

I have a question related to testing whether two types are equal or not. The utility type in @type-challenge is as follows :

export type Equals<X, Y> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2) ? true : false;

Maybe it a very basic or idiotic question but i am unable to understand from where this T type parameter is coming from and what will be its values

shayanpaul
Автор

Awesome content. Keep up the great work!

ffedchik
Автор

You could do something like this instead of using overloads on functions:

```ts

interface Item {
id: number
}
interface FindOptions {
one: boolean
}
declare function find<Options extends FindOptions>(options: Options): Options extends {one: infer FindOne} ? FindOne extends true ? Item: Item[] : never

const item = find({one: true}) // Item
const items = find({one: false}) // Item[]

```

Edit:

After a few minutes I realized that in the above case there is no need because I could just do `Options extends {one: true}` directly but imagine the possibilities 😁

samirnayibruza
Автор

type MyParameters<
T extends (...args: any[]) => any,
I extends number
> = T extends (...args: infer A) => any ? A[I] : never;

MohamedRagab-kpbp
Автор

Thank you for this. Agreed with Jimmy this is hard.

yujitomita