Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Type Inference | Type Inference and Advanced Type Safety
TypeScript Types Fundamentals

bookType Inference

When you declare variables or functions in TypeScript without explicitly stating their types, TypeScript uses type inference to determine the types for you.

123456789
let message = "Hello, TypeScript!"; let count = 5; let isActive = true; function add(a, b) { return a + b; } const result = add(2, 3);
copy

In the code above, message is assigned a string value, so TypeScript infers its type as string. The variable count is set to a number, so its type is inferred as number. Similarly, isActive is inferred as a boolean because it is assigned a boolean value.

For the function add, there are no explicit type annotations for its parameters or return value. TypeScript examines the function's implementation and its usage. When you call add(2, 3), both arguments are numbers, so TypeScript infers that a and b are likely numbers, and the return type is also number. As a result, the variable result is inferred to be a number.

Type inference helps you write less code while still getting the benefits of static type checking. However, when TypeScript cannot infer a clear type, it may use the any type, which removes type safety. It is important to be aware of what TypeScript is inferring in your code to avoid unexpected behavior.

question mark

Which of the following best describes the type that TypeScript infers for the variable result in the code above?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you explain more about when TypeScript uses the `any` type?

What are the benefits and drawbacks of relying on type inference?

How can I add explicit types to the variables and function in the example?

Awesome!

Completion rate improved to 8.33

bookType Inference

Svep för att visa menyn

When you declare variables or functions in TypeScript without explicitly stating their types, TypeScript uses type inference to determine the types for you.

123456789
let message = "Hello, TypeScript!"; let count = 5; let isActive = true; function add(a, b) { return a + b; } const result = add(2, 3);
copy

In the code above, message is assigned a string value, so TypeScript infers its type as string. The variable count is set to a number, so its type is inferred as number. Similarly, isActive is inferred as a boolean because it is assigned a boolean value.

For the function add, there are no explicit type annotations for its parameters or return value. TypeScript examines the function's implementation and its usage. When you call add(2, 3), both arguments are numbers, so TypeScript infers that a and b are likely numbers, and the return type is also number. As a result, the variable result is inferred to be a number.

Type inference helps you write less code while still getting the benefits of static type checking. However, when TypeScript cannot infer a clear type, it may use the any type, which removes type safety. It is important to be aware of what TypeScript is inferring in your code to avoid unexpected behavior.

question mark

Which of the following best describes the type that TypeScript infers for the variable result in the code above?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 1
some-alt