Length of Tuple
#
introductionFor given a tuple, you need create a generic Length
, pick the length of the tuple
For example
ts
type tesla = ['tesla', 'model 3', 'model X', 'model Y'];type spaceX = ['FALCON 9', 'FALCON HEAVY', 'DRAGON', 'STARSHIP', 'HUMAN SPACEFLIGHT'];type teslaLength = Length<tesla>; // expected 4type spaceXLength = Length<spaceX>; // expected 5
View on GitHubts
type tesla = ['tesla', 'model 3', 'model X', 'model Y'];type spaceX = ['FALCON 9', 'FALCON HEAVY', 'DRAGON', 'STARSHIP', 'HUMAN SPACEFLIGHT'];type teslaLength = Length<tesla>; // expected 4type spaceXLength = Length<spaceX>; // expected 5
#
start pointtsTry
/* _____________ Your Code Here _____________ */typeLength <T extends any> = any;Â/* _____________ Test Cases _____________ */consttesla = ['tesla', 'model 3', 'model X', 'model Y'] as const;constspaceX = ['FALCON 9','FALCON HEAVY','DRAGON','STARSHIP','HUMAN SPACEFLIGHT',] as const;Âtypecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Length <typeoftesla >, 4>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Length <typeofspaceX >, 5>>,];
Take the ChallengetsTry
/* _____________ Your Code Here _____________ */typeLength <T extends any> = any;Â/* _____________ Test Cases _____________ */consttesla = ['tesla', 'model 3', 'model X', 'model Y'] as const;constspaceX = ['FALCON 9','FALCON HEAVY','DRAGON','STARSHIP','HUMAN SPACEFLIGHT',] as const;Âtypecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Length <typeoftesla >, 4>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Length <typeofspaceX >, 5>>,];