Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Type Annotations and Type Checking | Understanding TypeScript Types
TypeScript Types Fundamentals

bookType Annotations and Type Checking

12345678
// Variable declared with type annotation let age: number = 30; // Variable declared without type annotation (TypeScript infers the type) let userName = "Alice"; // Type error: assigning a string to a number variable // age = "thirty"; // Error: Type 'string' is not assignable to type 'number'
copy

TypeScript allows you to declare the type of a variable using a type annotation, as shown with let age: number = 30;. Here, you explicitly tell TypeScript that age must always hold a number. When you declare a variable without a type annotation, like let userName = "Alice";, TypeScript uses type inference to determine that name is a string based on the assigned value.

TypeScript performs type checking at compile time, meaning it analyzes your code before it runs. If you try to assign a value of the wrong typeβ€”such as assigning a string to the age variableβ€”TypeScript immediately reports an error and prevents the code from compiling. This compile-time type checking helps you catch mistakes early and write safer, more predictable code.

question mark

What is the purpose of a type annotation in TypeScript, as shown in let age: number = 30;?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain more about type inference in TypeScript?

What happens if I don't specify a type and don't assign a value?

Are there other ways to declare variable types in TypeScript?

Awesome!

Completion rate improved to 8.33

bookType Annotations and Type Checking

Swipe to show menu

12345678
// Variable declared with type annotation let age: number = 30; // Variable declared without type annotation (TypeScript infers the type) let userName = "Alice"; // Type error: assigning a string to a number variable // age = "thirty"; // Error: Type 'string' is not assignable to type 'number'
copy

TypeScript allows you to declare the type of a variable using a type annotation, as shown with let age: number = 30;. Here, you explicitly tell TypeScript that age must always hold a number. When you declare a variable without a type annotation, like let userName = "Alice";, TypeScript uses type inference to determine that name is a string based on the assigned value.

TypeScript performs type checking at compile time, meaning it analyzes your code before it runs. If you try to assign a value of the wrong typeβ€”such as assigning a string to the age variableβ€”TypeScript immediately reports an error and prevents the code from compiling. This compile-time type checking helps you catch mistakes early and write safer, more predictable code.

question mark

What is the purpose of a type annotation in TypeScript, as shown in let age: number = 30;?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 2
some-alt