Type assignment and type inference play crucial roles in defining and working with variables. Let's explore both concepts:
- 1. Type Assignment:
- Example:
- 2. Type Inference:
- Example:
- 3. Combining Type Assignment and Type Inference:
- Example:
- 4. Function Parameter and Return Type Annotations:
- Example:
Type assignment involves explicitly specifying the type of a variable. This is done using a colon (:
) followed by the type name.
// Type assignment for numbers
let age: number = 25;
// Type assignment for strings
let name: string = "Alice";
// Type assignment for booleans
let isValid: boolean = true;
// Type assignment for arrays
let numbers: number[] = [1, 2, 3];
Type inference is a TypeScript feature that automatically determines the type of a variable based on its initialization. When a variable is assigned a value during declaration, TypeScript infers its type without explicit type annotations.
// Type inference for numbers
let height = 180; // TypeScript infers the type as number
// Type inference for strings
let greeting = "Hello"; // TypeScript infers the type as string
// Type inference for booleans
let isTrue = false; // TypeScript infers the type as boolean
// Type inference for arrays
let fruits = ["apple", "orange", "banana"]; // TypeScript infers the type as string[]
Type inference simplifies code by reducing the need for explicit type annotations. However, explicit type annotations can still be beneficial for clarity and documentation.
In many cases, you might use a combination of type assignment and type inference.
// Type assignment with type inference
let employeeId: number = 123;
let employeeName = "John Doe"; // TypeScript infers the type as string
// Type assignment with complex types
let userDetails: { id: number; name: string } = {
id: employeeId,
name: employeeName,
};
Type assignment is commonly used when defining function parameter types and return types.
// Function with type annotations
function addNumbers(x: number, y: number): number {
return x + y;
}
// Type inference for function return type
function multiply(x: number, y: number) {
return x * y; // TypeScript infers the return type as number
}
Both type assignment and type inference contribute to the language's static typing features, helping catch potential errors during development and providing better tooling support. Choose the approach that fits the context of your code and enhances readability.