Our first look at INFER - Type Challenges #4

preview_player
Показать описание
Become a TypeScript Wizard with Matt's upcoming TypeScript Course:

Follow Matt on Twitter

Рекомендации по теме
Комментарии
Автор

I did this by doing type First<TArray extends any[]> = TArray extends { length: 0 } ? never: TArray[0]. But the infer explanation is really really helpful. Thank you!

rgolea
Автор

I just wanted to say thanks for posting awesome and digestible TS content. I'm looking forward to going through your advanced course when it's available. I feel like the barrier to learning how to build "Library world" types is immensely more challenging than learning TS as a user.

dchroninger
Автор

I saw an answer that was just "T extends Array<never> ? never : T[0];" that works which is pretty interesting because an empty array must be treated as Array<never>. That might be a pretty useful short syntax for handling that case.

michrisoft
Автор

Whenever I feel that I already know enough TS I come back here and realize I'm just like Jon Snow, I know nothing. Thanks for that Matt.

fredcal
Автор

Since I knew of T[0] from last time, this was simple "= T extends [] ? never : T[0];"
Dunno if anything can extend [] (but in that case I probably can reverse the order).

But I managed to sneak infer into TupleToObject and get my recursion 😋

gulneckm.
Автор

Cool! Thank you! Waiting for other type challenges :)

nght
Автор

I used this one:
type First<TTuple extends readonly any[]> = TTuple['length'] extends 0 ? never : TTuple[0];

It's always been difficult for me to grasp when I can use infer. Thank you so much for this explanation :)

bluesbr
Автор

I love the way you describe and solve the problem that it hits your mind directly and make everything understandable!!
Keep it up 👍

alihussnain
Автор

I did something very similar with

type First<Ts extends unknown[]> = Ts extends []
? never
: Ts[0] | (Ts extends [unknown, ...unknown[]]
? never
: undefined
);

with the main difference being, that if the array is not a tuple, but an unknown length (T[]) I thought it would make sense to return `T | undefined` instead of never, as it may or may not hold a first element.

FurryDanOriginal
Автор

Thank you for the enjoyable and informative videos, they've also saved a lot of time for me. Keep doing your cool stuff!

istvanagoston
Автор

the summer is ending soon, when your course on advanced Typescript will be released !?

yassinebouchoucha
Автор

Hey Matt, I'm in love with your videos and learning a lot because tbh I''
m not a big fan of TS but I'm giving it a chance. So, I couldn't remember an example in other languages and I'm not sure if this behavior is only in TS, but it seems to be something like => You're creating a feature or functionality just specifying a type? In other languages we need to call a function that belongs to a class, e.g List<string> in Java we have List.first or List.at(0). I'm trying to get used with this way to create functionalities via defining a type, it's pretty weird to me because type should just be a type.

matheusviniciusandrade
Автор

Actually, this doesn't work for me when i declare a const array and use it as follows: First<typeof arrayVariableName>. It always resolves to never. Btw, thanks for high quality materials, they really help a lot!

MrBatocko
Автор

never is like 'imaginary number' of typescript

kilo.ironblossom
Автор

A simpler version is:

type First <TArray extends any[]> = TArray extends [] ? never : TArray[0]

PeterParker-nhdi
Автор

type First<T extends any[]> = T extends T["length"] ? T[0] : never - why is this not working? T length is > 0 so its true and should take the first index of T otherwise its never. Can you maybe help me ? The Result of this is always never :/

cobrax
Автор

where can i find those exercises? Id like to practice

themost
Автор

How would this be used in a project? What can I do with the newly created type?

claustw
Автор

Can anybody point me in the direction to where I can read more about "TArray". I guess it's a concept that you can apply to other stuff than an array. I just can't seem to find anything in the TS documentation.

JakobBjerrePetersen
Автор

Don't want to get banned.. So, I will just say Hello this time.

ColinRichardson