type-challenges icon indicating copy to clipboard operation
type-challenges copied to clipboard

15 - Last of Array

Open ashi009 opened this issue 5 years ago • 27 comments

type Last<T extends any[]> = [any, ...T][T["length"]];

ashi009 avatar Sep 04 '20 04:09 ashi009

Does not seem to be working or I am doing something worng here?

https://www.typescriptlang.org/play?#code/C4TwDgpgBAMghgZ2AHgCpQgD2BAdgEwSjlxAG0BdAPigF4oySQAaKAOg9QrNTICIANngDmwABZ8KFANwAoWQGMA9riRQARnABOALliIUSLQEtcwyjXoAWaUA

vydimitrov avatar Nov 26 '20 07:11 vydimitrov

Great, very neat and clever!

mleg avatar Dec 18 '20 14:12 mleg

clever... What the fuck , Typescript

GreatAuk avatar Jan 29 '21 10:01 GreatAuk

You are genius!!!😂

Ahmed364051 avatar Apr 06 '23 13:04 Ahmed364051

I ended up with the other solution, which uses infer. I have a question though: why can't we use T[T["length"]]? It gives me an error when I try it =/

paulopontovaz avatar Apr 20 '23 15:04 paulopontovaz

I ended up with the other solution, which uses infer. I have a question though: why can't we use T[T["length"]]? It gives me an error when I try it =/

I guess it's because the length index overflows

daolanfler avatar Apr 28 '23 02:04 daolanfler

I ended up with the other solution, which uses infer. I have a question though: why can't we use T[T["length"]]? It gives me an error when I try it =/

you need T[T.length-1], so the any is to shift the array once to the right

GideonMax avatar May 16 '23 08:05 GideonMax

Does not seem to be working or I am doing something worng here?

https://www.typescriptlang.org/play?#code/C4TwDgpgBAMghgZ2AHgCpQgD2BAdgEwSjlxAG0BdAPigF4oySQAaKAOg9QrNTICIANngDmwABZ8KFANwAoWQGMA9riRQARnABOALliIUSLQEtcwyjXoAWaUA

that's because T["length"] is number since there is no predefined length, and since the first element of [any,...T] is any Last<string[]> is any

GideonMax avatar May 16 '23 09:05 GideonMax

Does not seem to be working or I am doing something worng here?

https://www.typescriptlang.org/play?#code/C4TwDgpgBAMghgZ2AHgCpQgD2BAdgEwSjlxAG0BdAPigF4oySQAaKAOg9QrNTICIANngDmwABZ8KFANwAoWQGMA9riRQARnABOALliIUSLQEtcwyjXoAWaUA

type Last<T extends any[]> = [T extends (infer NT)[] ? NT : never, ...T][T["length"]]; should work.

EltonLobo07 avatar May 21 '23 16:05 EltonLobo07

Im inspired me thank you

WanderedToLa avatar May 26 '23 01:05 WanderedToLa

What about never:

type Last<T extends any[]> = [never, ...T][T["length"]];

KNHui avatar Sep 25 '23 14:09 KNHui

type Last<T extends unknown[]> = [unknown, ...T][T["length"]]

this is also working

jericbas avatar Nov 04 '23 13:11 jericbas

卧槽 牛逼

nomoreyou avatar Dec 06 '23 02:12 nomoreyou

type Last<arr extends any[]> = arr extends [...leftProps: any[], infer L] ? L : never

this is also working

huanganfree avatar Dec 13 '23 08:12 huanganfree

type Last<T extends any[]> = T extends [...any, infer L] ? L : T

this is also working

jinyoung234 avatar Jan 01 '24 06:01 jinyoung234

type Last<T extends any[]> = T extends [...infer A, infer B] ? B : never

Qzhor avatar Jan 03 '24 08:01 Qzhor

卧槽 牛逼

nb

Zum-Gluck avatar Feb 02 '24 02:02 Zum-Gluck

type Last<T extends any[]> = T extends [infer F, ...infer R] ? R["length"] extends 0 ? F : Last<R> : never

dbfu avatar Feb 02 '24 13:02 dbfu

FYI, subtract operation on types is not natively supported. You can actually find type challenges for this: 2257・MinusOne 7561・Subtract

MarvinXu avatar Mar 23 '24 09:03 MarvinXu

wow...

im-jihoon avatar Apr 20 '24 10:04 im-jihoon

You are genius!!!

QingCT avatar May 29 '24 02:05 QingCT

Think Different

keatkeat87 avatar Jul 06 '24 13:07 keatkeat87

I'm so stupid

type Last<T extends Array<any>> = T extends [T[0]] ? T[0] : (
  T extends [T[0], ...infer U] ? Last<U> : never
); 

TaR1z avatar Aug 08 '24 07:08 TaR1z