Junior — Middle
What is the difference between the undefined and any types in TypeScript?
sobes.tech AI
Answer from AI
In TypeScript, undefined is a specific type and value that indicates a variable has not been initialized or has been explicitly assigned the value undefined.
any is a special type that disables type checking for a variable, allowing it to accept values of any type without compilation errors.
Example:
let a: undefined = undefined; // a can only be undefined
let b: any = 5; // b can be anything
b = "string"; // allowed
b = undefined; // also allowed
Thus, undefined is a specific value and type, while any is a universal type that allows bypassing the type system.