Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Annotating Variables and Constants | Transitioning from JavaScript to TypeScript
TypeScript for JavaScript Developers

bookAnnotating Variables and Constants

When you move from JavaScript to TypeScript, one of the most immediate changes you encounter is the ability to explicitly annotate the types of your variables and constants. In JavaScript, variables can hold any type of value at any time, which can lead to subtle bugs if a value is accidentally reassigned to an unexpected type.

TypeScript addresses this by allowing you to specify the type of a variable or constant when you declare it. The most common primitive types you will use are string, number, and boolean. You can annotate variables by adding a colon and the type after the variable name. Explicit annotations are especially useful when you want to make your intent clear, prevent accidental reassignment, or when the type cannot be immediately inferred from the assigned value.

123456789101112131415
// JavaScript: dynamic typing let username = "alice"; let age = 30; let isAdmin = false; // These variables can be reassigned to any type later username = 42; // No error in JavaScript // TypeScript: explicit type annotations let username: string = "alice"; let age: number = 30; let isAdmin: boolean = false; // TypeScript will show an error if you try: username = 42; // Error: Type 'number' is not assignable to type 'string'
copy
12345678
// Defining configuration constants for an application const API_URL: string = "https://api.example.com"; const MAX_CONNECTIONS: number = 5; const ENABLE_LOGGING: boolean = true; // TypeScript ensures these are not misconfigured: const TIMEOUT: string = 3000; // Error: Type 'number' is not assignable to type 'string'
copy

TypeScript can often automatically determine the type of a variable based on the value you assign to it. This is called type inference. For example, if you write let city = "New York";, TypeScript knows city is a string and will prevent you from assigning a number to it later. You do not always need to annotate every variable; type inference works well when the initial value makes the type obvious. However, explicit annotations are helpful when the type is not clear, such as when a variable is declared but not initialized, or when you want to enforce a specific type for documentation or safety reasons.

1. When should you use explicit type annotations for variables in TypeScript?

2. What is type inference in TypeScript?

question mark

When should you use explicit type annotations for variables in TypeScript?

Select the correct answer

question mark

What is type inference in TypeScript?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 4

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Awesome!

Completion rate improved to 7.14

bookAnnotating Variables and Constants

Desliza para mostrar el menú

When you move from JavaScript to TypeScript, one of the most immediate changes you encounter is the ability to explicitly annotate the types of your variables and constants. In JavaScript, variables can hold any type of value at any time, which can lead to subtle bugs if a value is accidentally reassigned to an unexpected type.

TypeScript addresses this by allowing you to specify the type of a variable or constant when you declare it. The most common primitive types you will use are string, number, and boolean. You can annotate variables by adding a colon and the type after the variable name. Explicit annotations are especially useful when you want to make your intent clear, prevent accidental reassignment, or when the type cannot be immediately inferred from the assigned value.

123456789101112131415
// JavaScript: dynamic typing let username = "alice"; let age = 30; let isAdmin = false; // These variables can be reassigned to any type later username = 42; // No error in JavaScript // TypeScript: explicit type annotations let username: string = "alice"; let age: number = 30; let isAdmin: boolean = false; // TypeScript will show an error if you try: username = 42; // Error: Type 'number' is not assignable to type 'string'
copy
12345678
// Defining configuration constants for an application const API_URL: string = "https://api.example.com"; const MAX_CONNECTIONS: number = 5; const ENABLE_LOGGING: boolean = true; // TypeScript ensures these are not misconfigured: const TIMEOUT: string = 3000; // Error: Type 'number' is not assignable to type 'string'
copy

TypeScript can often automatically determine the type of a variable based on the value you assign to it. This is called type inference. For example, if you write let city = "New York";, TypeScript knows city is a string and will prevent you from assigning a number to it later. You do not always need to annotate every variable; type inference works well when the initial value makes the type obvious. However, explicit annotations are helpful when the type is not clear, such as when a variable is declared but not initialized, or when you want to enforce a specific type for documentation or safety reasons.

1. When should you use explicit type annotations for variables in TypeScript?

2. What is type inference in TypeScript?

question mark

When should you use explicit type annotations for variables in TypeScript?

Select the correct answer

question mark

What is type inference in TypeScript?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 4
some-alt