The 'any' Type: Escape Hatch
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 7 of 73.
Sometimes you need to work with data where you don't know the exact type in advance, or you're migrating JavaScript code to TypeScript gradually. For these situations, TypeScript provides the any type - your escape hatch from type checking.
The any type allows a variable to hold any value and disables TypeScript's type checking for that variable:
let data: any = 42;
data = "Hello"; // No error
data = true; // No error
data.foo.bar.baz; // No error (even though this might crash at runtime)Challenge
EasyDeclare a variable named dynamicData with the type any and assign it the number 42.
Then reassign dynamicData to the string "Hello World" to demonstrate the flexibility of the any type.
Finally, print the variable to the console to show its current value.
Try it yourself
// TODO: Write your code hereThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To TypeScript
1Getting Started with TS
What is TypeScript?Why Use TypeScript?Your First TypeScript CodeCompilation Process & ErrorsRecap: Introduction to TS4Working with Functions
Typing Params & Return ValuesTyping Arrow FunctionsThe 'void' Return TypeOptional Parameters with '?'Default Parameter ValuesTyping Rest ParametersDefining Function TypesRecap: Building Typed Funcs2Core Types
Basic Types: str, num, booleanThe 'any' Type: Escape HatchThe 'unknown' TypeWorking with 'null' & 'undef'Type Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Types: Aliases, Unions & Inter
Type Aliases for PrimitivesUnion Types ('|')Working with Union TypesLiteral TypesIntersection Types ('&')Combining Type AliasesRecap: Advanced Type Combos3Data Structure: Arrays & Tuple
Typed Arrays'readonly' Modifier for ArraysWhat is a Tuple?Declaring and Accessing TuplesDestructuring TuplesReadonly TuplesMulti-dimensional Typed Arrays Spread Operator with ArraysRecap: Arrays and Tuples