Recap: Defining Object Shapes
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 44 of 73.
Challenge
EasyCreate an interface named Computer with the following properties and methods:
idof typenumber(readonly)brandof typestringramInGBof typenumber(optional)isLaptopof typebooleanbootmethod that takes no parameters and returnsvoid
Create an interface named Server with the following properties and methods:
hostnameof typestring(readonly)operatingSystemof typestringmaxConnectionsof typenumber(optional)isOnlineof typebooleanrestartmethod that takes no parameters and returnsvoidgetStatusmethod that takes no parameters and returns astring
Using your interfaces, create the following variables:
- Create a variable named
workstationof typeComputerwith id1001, brand"Dell", ramInGB16, isLaptopfalse, and abootmethod that prints"Dell workstation is booting up..." - Create a variable named
laptopof typeComputerwith id1002, brand"MacBook", isLaptoptrue, and abootmethod that prints"MacBook laptop is starting..." - Create a variable named
webServerof typeServerwith hostname"web-01", operatingSystem"Ubuntu", maxConnections1000, isOnlinetrue, arestartmethod that prints"Restarting web-01 server...", and agetStatusmethod that returns"web-01 is online"
Create a function named checkComputerSpecs that accepts a parameter of type Computer and returns a string. The function should return the computer's brand and type in the format "[brand] [laptop/desktop]" (use "laptop" if isLaptop is true, otherwise use "desktop").
Create a function named getServerInfo that accepts a parameter of type Server and returns a string in the format "[hostname] runs [operatingSystem]".
Print the following outputs on separate lines:
- Call the
bootmethod onworkstation - Call the
bootmethod onlaptop - Call
checkComputerSpecswithworkstationand print the result - Call
checkComputerSpecswithlaptopand print the result - Call the
getStatusmethod onwebServerand print the result - Call
getServerInfowithwebServerand print the result - Print the id of
workstation - Call the
restartmethod onwebServer
Try it yourself
// TODO: Write your code here
// Create the Computer interface with the required properties and methods
// Create the Server interface with the required properties and methods
// Create the workstation, laptop, and webServer variables
// Create the checkComputerSpecs and getServerInfo functions
// Print all 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 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 Shapes