TypeScript Academy · Lesson

Type Inference & any vs unknown

See how TypeScript infers types, the difference between any and unknown, and how const changes inference.

Lesson 2 of 38 steps

Type Inference & any vs unknown is a free TypeScript Academy lesson on CoddyKit. This is lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The TypeScript Academy course includes 3 lessons in total.

Intro

Goal: Learn how TS infers types from values, and compare any vs unknown for safety.

Inference with let & const

Inference: let infers a broad type (number). const infers a literal type ("TS").

let count = 5;       
 // inferred number
const title = "TS"; 
// inferred literal type "TS"
console.log(count, title);

as const

as const preserves literal types, freezing values as readonly. Useful for enums and config arrays.

const colors = ["red", "green", "blue"] as const;

// type is readonly ["red","green","blue"]

console.log(colors);

any type

any disables type checking. It gives freedom but removes safety: mistakes compile and crash at runtime.

let flexible: any = 42;

lexible = "hello";

flexible.nonexistentMethod(); 

// no error at compile time!

unknown type

unknown forces you to check type before usage. Safer than any because you cannot call methods blindly.

let input: unknown = "hello";

// Must narrow before use

if (typeof input === "string") { 
    console.log(input.toUpperCase());
}

When to use

Guideline: Prefer inference for most variables. Use explicit annotations for clarity. Avoid any; prefer unknown when type is not known yet.

any vs unknown check

Quick check: What is the main difference between any and unknown?

Recap

Recap: TS infers types automatically. const narrows to literals, as const locks values. any disables safety; unknown enforces checks.

Free to start

Learn TypeScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
101
Lessons
352

Frequently Asked Questions

Is the “Type Inference & any vs unknown” lesson free?

Yes — the full text of “Type Inference & any vs unknown” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO. The TypeScript Academy course includes 3 lessons in total.

What will I learn in “Type Inference & any vs unknown”?

See how TypeScript infers types, the difference between any and unknown, and how const changes inference. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.

How long does the “Type Inference & any vs unknown” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Primitive Types & Annotations
  2. Type Inference & any vs unknown
  3. Union & Literal Types (basic narrowing intro)
← Back to TypeScript Academy