Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende The any and unknown Types | Type Inference and Advanced Type Safety
TypeScript Types Fundamentals

bookThe any and unknown Types

When you use the any type, you tell TypeScript to turn off type checking for that variable. This means you can assign any value to it and perform any operation, but you lose all benefits of type safety.

On the other hand, the unknown type is safer. Like any, you can assign any value to a variable of type unknown. However, you cannot use the value in operations until you perform a type check. TypeScript forces you to check the type before using it, which helps prevent errors.

1234567891011121314151617
let valueAny: any = "Hello"; let valueUnknown: unknown = "World"; // You can assign any value to a variable of type any or unknown valueAny = 42; valueUnknown = 42; // You can perform any operation on a value of type any console.log(valueAny.toFixed(2)); // No error at compile time // But you cannot perform operations on unknown without type checking // console.log(valueUnknown.toFixed(2)); // Error: Object is of type 'unknown' // To use valueUnknown safely, you must first check its type if (typeof valueUnknown === "number") { console.log(valueUnknown.toFixed(2)); }
copy

In the example above, valueAny can be used in any way, and TypeScript will not warn you about possible mistakes. This can lead to runtime errors that TypeScript cannot catch.

In the example, trying to call toFixed on valueUnknown causes a compile-time error unless you first check that it is a number.

You should use unknown instead of any when you do not know the type of a value ahead of time, but want to make sure you check it before using it. This keeps your code safer and more predictable.

question mark

When should you prefer using the unknown type over any?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 3

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 8.33

bookThe any and unknown Types

Desliza para mostrar el menú

When you use the any type, you tell TypeScript to turn off type checking for that variable. This means you can assign any value to it and perform any operation, but you lose all benefits of type safety.

On the other hand, the unknown type is safer. Like any, you can assign any value to a variable of type unknown. However, you cannot use the value in operations until you perform a type check. TypeScript forces you to check the type before using it, which helps prevent errors.

1234567891011121314151617
let valueAny: any = "Hello"; let valueUnknown: unknown = "World"; // You can assign any value to a variable of type any or unknown valueAny = 42; valueUnknown = 42; // You can perform any operation on a value of type any console.log(valueAny.toFixed(2)); // No error at compile time // But you cannot perform operations on unknown without type checking // console.log(valueUnknown.toFixed(2)); // Error: Object is of type 'unknown' // To use valueUnknown safely, you must first check its type if (typeof valueUnknown === "number") { console.log(valueUnknown.toFixed(2)); }
copy

In the example above, valueAny can be used in any way, and TypeScript will not warn you about possible mistakes. This can lead to runtime errors that TypeScript cannot catch.

In the example, trying to call toFixed on valueUnknown causes a compile-time error unless you first check that it is a number.

You should use unknown instead of any when you do not know the type of a value ahead of time, but want to make sure you check it before using it. This keeps your code safer and more predictable.

question mark

When should you prefer using the unknown type over any?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 3
some-alt