JavaScript > TypeScript > TypeScript Basics > Type annotations
TypeScript Type Annotations: Declaring Variable Types
This snippet demonstrates how to use type annotations in TypeScript to explicitly define the types of variables. Understanding type annotations is fundamental to leveraging TypeScript's static typing capabilities, enabling early error detection and improved code maintainability.
Basic Type Annotations
This code showcases how to annotate variables with specific types. number
, string
, and boolean
are primitive types. Arrays are annotated using type[]
. Objects are annotated by defining the structure of the object with the types of its properties. Functions are annotated by defining the types of their parameters and the return type.
// Explicitly defining variable types
let age: number = 30;
let name: string = 'John Doe';
let isStudent: boolean = false;
// Type annotation for arrays
let numbers: number[] = [1, 2, 3, 4, 5];
// Type annotation for objects
let person: { name: string; age: number } = { name: 'Jane Doe', age: 25 };
// Type annotation for functions
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet(name));
Concepts Behind Type Annotations
Type annotations are a way to explicitly tell the TypeScript compiler what type a variable is expected to be. This allows TypeScript to perform static type checking, catching errors at compile time rather than runtime. Without type annotations, TypeScript attempts to infer the type based on the value assigned to the variable. While type inference is helpful, explicit annotations improve code clarity and help prevent unexpected type-related errors.
Real-Life Use Case
Imagine building a complex web application with numerous components. Using type annotations, you can ensure that data passed between components adheres to a specific structure. For example, when fetching user data from an API, you can define an interface with the expected properties and types. This ensures that the data received is in the correct format and prevents runtime errors when accessing properties.
Example interface
This interface ensures that any data assigned to the `User` interface must have the id (number), name (string) and email (string) fields.
interface User {
id: number;
name: string;
email: string;
}
Best Practices
Interview Tip
Be prepared to explain the benefits of type annotations over JavaScript's dynamic typing. Emphasize the ability to catch errors early, improve code maintainability, and enhance code readability.
When to Use Them
Use type annotations when:
Memory Footprint
Type annotations themselves do not directly impact the memory footprint of the compiled JavaScript code. TypeScript type information is primarily used during development and compilation and is stripped away during the compilation process. The generated JavaScript code will have a memory footprint similar to that of JavaScript code without type annotations.
Alternatives
Pros
Cons
FAQ
-
What happens if I assign a value of the wrong type to a variable with a type annotation?
The TypeScript compiler will raise a type error, preventing you from compiling the code until you fix the type mismatch. -
Are type annotations required in TypeScript?
No, TypeScript can infer types in many cases. However, using type annotations is a good practice for clarity and to catch errors early. -
Do type annotations affect the runtime performance of my code?
No, type annotations are removed during the compilation process, so they do not affect the runtime performance of the generated JavaScript code.