Recap: Using Enums
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 56 of 73.
Challenge
EasyCreate a string enum named OrderStatus with four members:
Pendinginitialized to"Pending"Shippedinitialized to"Shipped"Deliveredinitialized to"Delivered"Cancelledinitialized to"Cancelled"
Create an interface named Order with the following properties:
idof typenumbercustomerNameof typestringstatusof typeOrderStatus
Create three order objects using the Order interface:
order1with id1001, customerName"Alice Johnson", and statusOrderStatus.Pendingorder2with id1002, customerName"Bob Smith", and statusOrderStatus.Shippedorder3with id1003, customerName"Carol Davis", and statusOrderStatus.Delivered
Create a function named displayOrderInfo that takes one parameter:
orderof typeOrder
The function should print a message in the format: "Order #[id] for [customerName] is [status]" and have a return type of void.
Print the following outputs:
- Call
displayOrderInfowithorder1 - Call
displayOrderInfowithorder2 - Call
displayOrderInfowithorder3 - Print the value of
OrderStatus.Cancelled - Print the value of
order2.status
Try it yourself
// TODO: Write your code here
// Create the OrderStatus enum, Order interface, order objects, and displayOrderInfo function
// Print the required outputsAll 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 Tuples