Recap: Generic Function Prac
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 62 of 73.
Challenge
EasyCreate a generic function named makePair that takes two arguments of different types and returns them as a tuple.
The function should:
- Use two generic type parameters
TandU - Accept a parameter
firstof typeT - Accept a parameter
secondof typeU - Return a tuple of type
[T, U] - Have an explicit return type annotation
Test your function by creating the following variables:
stringNumberPair- callmakePairwith"Hello"and42booleanStringPair- callmakePairwithtrueand"World"numberBooleanPair- callmakePairwith100andfalse
Use destructuring to extract values from your tuples:
- Destructure
stringNumberPairinto variablestextandnum - Destructure
booleanStringPairinto variablesflagandmessage
Print the following outputs:
- Print
text - Print
num - Print
flag - Print
message - Print the first element of
numberBooleanPair - Print the second element of
numberBooleanPair - Print the result of calling
makePairwith"TypeScript"and2024, accessing the first element - Print the result of calling
makePairwith99and"bottles", accessing the second element
Try it yourself
// TODO: Write your code here
// Create the generic makePair function with type parameters T and U
// The function should accept two parameters and return a tuple
// TODO: Test your function by creating the required variables
// stringNumberPair, booleanStringPair, numberBooleanPair
// TODO: Use destructuring to extract values from tuples
// Destructure stringNumberPair and booleanStringPair as specified
// TODO: Print all the required outputs
// Print text, num, flag, message, and the other required valuesAll lessons in Introduction To TypeScript
1Getting Started with TS
What is TypeScript?Why Use TypeScript?Your First TypeScript CodeCompilation Process & ErrorsRecap: Introduction to TS4Working with Functions
Typing Params & Return ValuesTyping Arrow FunctionsThe 'void' Return TypeOptional Parameters with '?'Default Parameter ValuesTyping Rest ParametersDefining Function TypesRecap: Building Typed Funcs2Core Types
Basic Types: str, num, booleanThe 'any' Type: Escape HatchThe 'unknown' TypeWorking with 'null' & 'undef'Type Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Types: Aliases, Unions & Inter
Type Aliases for PrimitivesUnion Types ('|')Working with Union TypesLiteral TypesIntersection Types ('&')Combining Type AliasesRecap: Advanced Type Combos3Data Structure: Arrays & Tuple
Typed Arrays'readonly' Modifier for ArraysWhat is a Tuple?Declaring and Accessing TuplesDestructuring TuplesReadonly TuplesMulti-dimensional Typed Arrays Spread Operator with ArraysRecap: Arrays and Tuples6Typing Objects and Interfaces
Inline Object Type AnnotationsType Aliases for ObjectsIntroduction to InterfacesInterfaces vs. Type AliasesOptional & Readonly PropsExtending Interfaces and TypesAdding Methods to InterfacesRecap: Defining Object Shapes9Generics: A First Look
The Problem Generics SolveCreating Generic Identity FuncUsing a Generic FunctionGeneric ArraysGeneric InterfacesRecap: Generic Function Prac