4 ways to use the TypeScript infer keyword
HTML-код
- Опубликовано: 27 май 2024
- 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.
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".
Lol thanks!
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...
Great job! This is definitely the best video about infer on 2022!
Upped the production quality. Looks dope
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 😀
Thank you. Perfect examples.
What a great explanation, thank you so much!
This was legitimately fun to code along with. Thank you!
Really nice demonstrations. Thanks a lot! It really made me understand the infer keyword better!
This was Epix.
Looking out for more.
This was the best explanation for infer. Cheers!
Awesome content. Keep up the great work!
This was cool. I didn't know anything about infer. Thanks to your video and challenges, now I know. Keep up the good work!
Dayum. This is very useful. Thanks for sharing.
Great explanation on this. Infer is so powerful
Wow. Awesome. Thanks Andrew.
superb tutorial!
Thanks for the video!
My implementation of FirstParam and SecondParam:
type FirstParameter any> = T extends (firstParam: infer A, ...args: any[]) => any ? A: never;
type SecondParam any> = T extends (firstParam: any, secondParam: infer A, ...args: any[]) => any ? A: never;
Also Jojo is everywhere LMAO
Thank you for this. Agreed with Jimmy this is hard.
Great video dude!! Thx you :D
you are a god kind sir, thank you!
This is good stuff.
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: 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 😁
awesome!
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 =
(() => T extends X ? 1 : 2) extends
(() => 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
Definitely not a dumb question, TypeScript's syntax here is pretty confusing. My understand is that, by declaring a generic parameter T (that declaration is the part), you're saying that T should be assigned when you call the function. Like this:
declare function oneOrTwo(): T extends string ? 1 : 2;
const x = oneOrTwo(); // 1
const y = oneOrTwo(); // 2, because T defaults to unknown
I found this really in-depth explanation of the whole Equals type, which might be helpful too: stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript
Thanks for watching!
Subbed!
type MyParameters<
T extends (...args: any[]) => any,
I extends number
> = T extends (...args: infer A) => any ? A[I] : never;
Tried to use typescript-challenges but getting an error on the import of the challenges utils. Is there something special that has to be done to get that working?
Hmm, don’t think so. Can you comment with a link to your playground?
Handsome man
omg, how do you know all that stuff!
💚
🤯🤯🤯🤯🤯
Typescript has become very complicated
lol
this is harder than rocket science
Nah, you got this! Let me know if you have questions, happy to help!
Nope. Too advanced. Need to back off a bit
Thanks for checking this out! Here’s one where I explain the details a little finely:
ruclips.net/video/wON6MCS0NkE/видео.html
I didn't understand shit
Weirdd explanation