0
0
Typescriptprogramming~15 mins

Strict configuration objects in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Strict configuration objects
📖 Scenario: You are building a simple app that uses a configuration object to control its behavior. To avoid mistakes, you want to make sure the configuration object only accepts specific properties and types.
🎯 Goal: Create a strict configuration object in TypeScript that only allows certain properties with exact types. Then, use this object in a function that reads the config and prints a message.
📋 What You'll Learn
Create a TypeScript interface called AppConfig with properties appName (string), version (string), and debug (boolean).
Create a constant config of type AppConfig with exact values: appName: 'MyApp', version: '1.0.0', debug: true.
Write a function printConfig that takes a parameter config of type AppConfig and prints the app name, version, and debug status.
Call printConfig with the config object and print the output.
💡 Why This Matters
🌍 Real World
Strict configuration objects help prevent bugs by ensuring only allowed settings are used in apps, making code safer and easier to maintain.
💼 Career
Many software jobs require writing clear and safe code. Using strict types for configuration is a common practice in professional TypeScript projects.
Progress0 / 4 steps
1
Create the AppConfig interface
Create a TypeScript interface called AppConfig with these exact properties: appName of type string, version of type string, and debug of type boolean.
Typescript
Need a hint?

Use the interface keyword to define the shape of the configuration object.

2
Create the config object
Create a constant called config of type AppConfig with these exact values: appName: 'MyApp', version: '1.0.0', and debug: true.
Typescript
Need a hint?

Use const and specify the type AppConfig for the config object.

3
Write the printConfig function
Write a function called printConfig that takes one parameter config of type AppConfig. Inside the function, use console.log to print: App: {appName}, Version: {version}, Debug mode: {debug} using the values from config.
Typescript
Need a hint?

Use a function with typed parameter and template strings for printing.

4
Call printConfig with config
Call the function printConfig with the config object to print the configuration details.
Typescript
Need a hint?

Simply call printConfig(config); to see the output.