CodeBlanks Logo Icon CodeBlanks

any type

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);  // true

any 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 error

any 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

index.ts
// 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ır

Sample solution

index.ts
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

Question
let deger: ____ = "merhaba";
deger = 100;
Answer
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

All lessons in Beginner