Recap: Fine-Tuning Types
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 73 of 73.
Challenge
EasyCreate a configuration management system that processes dynamic settings using index signatures, type guards, and type assertions.
Create an interface named AppConfig that uses an index signature to map string keys to values of type string | number | boolean.
Create a function named validateConfigValue that:
- Takes two parameters:
configof typeAppConfigandkeyof typestring - Returns
"missing"if the key doesn't exist in the config - Uses type guards to check the actual type of the value
- Returns
"string"if the value is a string - Returns
"number"if the value is a number - Returns
"boolean"if the value is a boolean - Has an explicit return type of
string
Create a function named getConfigAsString that:
- Takes two parameters:
configof typeAppConfigandkeyof typestring - Returns
"undefined"if the key doesn't exist - Uses a type assertion to treat the value as
stringand returns it if the value exists - Has an explicit return type of
string
Create a function named processConfig that:
- Takes two parameters:
configof typeAppConfigandkeyof typestring - Uses
validateConfigValueto check the type - Returns the value converted to uppercase if it's a string
- Returns the value multiplied by 10 if it's a number
- Returns the opposite boolean value if it's a boolean
- Returns
nullif the key is missing - Has an explicit return type of
string | number | boolean | null
Create test data:
- Create
settingsof typeAppConfigwith:"appName":"MyApp""version":2.1"debugMode":true"maxUsers":100"theme":"dark"
Test your functions and print the following outputs:
- Call
validateConfigValuewithsettingsand"appName" - Call
validateConfigValuewithsettingsand"version" - Call
validateConfigValuewithsettingsand"debugMode" - Call
validateConfigValuewithsettingsand"timeout" - Call
getConfigAsStringwithsettingsand"theme" - Call
getConfigAsStringwithsettingsand"missing" - Call
processConfigwithsettingsand"appName" - Call
processConfigwithsettingsand"maxUsers" - Call
processConfigwithsettingsand"debugMode" - Call
processConfigwithsettingsand"invalid"
Try it yourself
// TODO: Write your code here
// Create the AppConfig interface
// Create the validateConfigValue function
// Create the getConfigAsString function
// Create the processConfig function
// Create test data - settings object
// Test the functions and print results
console.log(validateConfigValue(settings, "appName"));
console.log(validateConfigValue(settings, "version"));
console.log(validateConfigValue(settings, "debugMode"));
console.log(validateConfigValue(settings, "timeout"));
console.log(getConfigAsString(settings, "theme"));
console.log(getConfigAsString(settings, "missing"));
console.log(processConfig(settings, "appName"));
console.log(processConfig(settings, "maxUsers"));
console.log(processConfig(settings, "debugMode"));
console.log(processConfig(settings, "invalid"));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 Combos8Enums
What is a Numeric Enum?Using Numeric EnumsWhat is a String Enum?Using String EnumsHeterogeneous EnumsRecap: Using Enums11Advanced Topics
Type Assertions Type Guards: in & instanceofThe 'never' TypeNullable Types ('strictNull')Index Signatures for ObjectsRecap: Fine-Tuning Types3Data 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