any type
- TypeScript course
- Beginner
- Lesson 25/89
- 4 min
Learn how the any type turns off type checking.
The any type
The any type completely disables type checking for a variable. A variable declared as any can be assigned any type of value and you can access any property on it. This turns off TypeScript's safety.
let x: any = 5;
x = "merhaba";
x = true;
console.log(x); // trueany is an escape hatch for data of unknown type, but it is unsafe.
any is unsafe
When any is used, TypeScript's type safety is lost. Misspelled property names do not cause compile errors and can cause runtime issues. If you do not know the type, using unknown is safer than any.
let x: any = "metin";
console.log(x.boyunUzunlugu); // undefined, no errorany should be used sparingly; prefer specific types whenever possible.
Practice Different types with any
Declare a variable named veri of type any. First assign it "test", then reassign it 42. Print the final value with console.log.
Starter code
// veri adında bir any değişkeni tanımla
// önce "test" değerini ata
// sonra 42 değerini yeniden ata
// son değeri yazdırSample solution
let veri: any = "test";
veri = 42;
console.log(veri);Expected output
42
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does the <code>any</code> type do?
- Prevents null assignment
- Disables type checking for a variable Correct answer
- Makes the variable constant
- Allows only string values
Explanation: <code>any</code> turns off all type checks on a variable; any type of value can be assigned and any property can be accessed.
2 Add the any type
let deger: ____ = "merhaba";
deger = 100;
let deger: any = "merhaba";
deger = 100;
Explanation: Since the variable takes both a string and a number, only <code>any</code> allows both assignments; <code>string</code> or <code>number</code> would fail on the other assignment.
3 any concepts
| Term | Meaning |
|---|---|
any |
Disables type checking |
let x: any = 5; x = "a"; |
Allows changing type |
any ile erişim |
Any property can be accessed |
any kaçış yolu |
Unsafe but flexible |