Type annotations in TypeScript allow you to explicitly specify the types of variables, function parameters, and return values. This helps catch errors early in the development process and makes your code more readable and maintainable.
Key Concepts
-
Basic Type Annotations:
- Variables: You can annotate variables with types.
- Function Parameters: You can specify the types of function parameters.
- Function Return Types: You can define the type of value a function returns.
-
Common Types:
number
string
boolean
array
object
any
void
null
andundefined
Basic Type Annotations
Variables
You can annotate variables with types using a colon followed by the type.
Function Parameters
Annotate function parameters by specifying the type after the parameter name.
Function Return Types
Specify the return type of a function after the parameter list.
Practical Examples
Example 1: Annotating Variables
let count: number = 10; let message: string = "Welcome to TypeScript!"; let isActive: boolean = false;
Example 2: Annotating Function Parameters and Return Types
function multiply(x: number, y: number): number { return x * y; } let result: number = multiply(5, 10); console.log(result); // Output: 50
Example 3: Annotating Arrays
Example 4: Annotating Objects
Exercises
Exercise 1: Annotate Variables
Annotate the following variables with appropriate types:
Solution:
Exercise 2: Annotate Function Parameters and Return Types
Annotate the following function parameters and return type:
Solution:
Exercise 3: Annotate Arrays and Objects
Annotate the following arrays and objects:
let fruits = ["Apple", "Banana", "Cherry"]; let car = { make: "Toyota", model: "Corolla", year: 2020 };
Solution:
let fruits: string[] = ["Apple", "Banana", "Cherry"]; let car: { make: string; model: string; year: number } = { make: "Toyota", model: "Corolla", year: 2020 };
Common Mistakes and Tips
-
Mistake: Forgetting to annotate function return types.
- Tip: Always specify the return type of functions to avoid unexpected results.
-
Mistake: Using
any
type excessively.- Tip: Use specific types whenever possible to leverage TypeScript's type-checking capabilities.
Conclusion
Type annotations are a fundamental feature of TypeScript that enhance code quality and maintainability. By explicitly specifying types for variables, function parameters, and return values, you can catch errors early and make your code more readable. Practice using type annotations in your projects to become proficient in TypeScript.
Next, we will explore how to compile TypeScript code in the "Compiling TypeScript" section.
TypeScript Course
Module 1: Introduction to TypeScript
- What is TypeScript?
- Setting Up the TypeScript Environment
- Basic Types
- Type Annotations
- Compiling TypeScript
Module 2: Working with Types
Module 3: Advanced Types
Module 4: Functions and Modules
Module 5: Asynchronous Programming
Module 6: Tooling and Best Practices
- Linting and Formatting
- Testing TypeScript Code
- TypeScript with Webpack
- TypeScript with React
- Best Practices