TypeScript | Type Annotation and Inference in TypeScript | Question4

Last Updated :
Discuss
Comments

What happens if you try to assign a string to a variable inferred as a number?

TypeScript
let x = 100;
x = "Hello";

The code compiles and runs without errors

TypeScript throws a compilation error

TypeScript changes the type of x to string

The value is automatically converted to a number

Share your thoughts in the comments