Recap: Advanced Type Combos
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 36 of 73.
Challenge
EasyCreate a type alias named Success for an object with a status property of literal type "success" and a data property of type any.
Create a type alias named Failure for an object with a status property of literal type "error" and a message property of type string.
Create a type alias named Result that is a union of the Success and Failure types.
Create a function named processResult that accepts a parameter called result of type Result and returns a string. The function should use a type guard on the status property to determine the result type and behave as follows:
- If the status is
"success", return"Operation successful: [data]" - If the status is
"error", return"Operation failed: [message]"
Create another function named handleApiResponse that accepts a parameter called response of type Result and returns a boolean. The function should return true if the operation was successful and false if it failed.
The following inputs will be provided:
- First input: a JSON string representing either a success result like
{"status": "success", "data": "User created"}or a failure result like{"status": "error", "message": "Invalid credentials"} - Second input: another JSON string in the same format
Parse both JSON inputs as Result objects, then:
- Call
processResultwith the first parsed result and print the returned string - Call
handleApiResponsewith the first parsed result and print the returned boolean - Call
processResultwith the second parsed result and print the returned string - Call
handleApiResponsewith the second parsed result and print the returned boolean
Try it yourself
import * as readline from 'readline';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const input: string[] = [];
// TODO: Write your code here
// 1. Create type aliases for Success, Failure, and Result
// 2. Create the processResult function
// 3. Create the handleApiResponse function
rl.on('line', (line: string) => {
input.push(line.trim());
if (input.length === 2) {
// Parse the JSON inputs
const result1 = JSON.parse(input[0]);
const result2 = JSON.parse(input[1]);
// 4. Call the functions with the parsed results and print the outputs
rl.close();
}
});All 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