TypeScript Survival Guide

Favorite typescript videos

051 — #17 Unknown Type in TypeScript | TypeScript Basics & Data Types | A Complete

The lecture introduces `unknown` alongside the already-familiar `any`: both accept assignment of any value (number, then string, then array — no compile error either way), but they differ completely on the way out. Assigning an `any` variable to a `string` compiles silently because `any` disables checking; assigning an `unknown` variable to a `string` errors with "unknown is not assignable to type

Video: 051 · #17 Unknown Type in TypeScript | TypeScript Basics & Data Types | A Complete · watch · ~07:01 · procademy

Source: this is a written companion to the video. Part of the Favorite TypeScript Videos curated path.

The lecture introduces unknown alongside the already-familiar any: both accept assignment of any value (number, then string, then array — no compile error either way), but they differ completely on the way out. Assigning an any variable to a string compiles silently because any disables checking; assigning an unknown variable to a string errors with “unknown is not assignable to type string.” The fix is a typeof narrowing guard — only assign inside if (typeof inputVal === "string") — demonstrated live: with a number stored the guard skips and the old name logs, after reassigning inputVal to “hello world” the guard passes and the name logs. The moral: use unknown when the stored type is not known up front but the eventual use is, because it keeps the code type-safe where any would not.

any and unknown both accept any assignment

Two more types round out the vocabulary — unknown and never — rarely used but worth knowing.

let inputVal: any = 100;
inputVal = "hello";
inputVal = [1, 2, 3]; // all fine

let inputVal2: unknown = 100;
inputVal2 = "hello";
inputVal2 = [1, 2, 3]; // all fine, no compile error

any is for holding any type of value; unknown is for when the stored data’s type is not known at declaration time.

The difference is on the way out — assignability

let uName: string = "something";
uName = inputVal; // inputVal: any → no error (checking disabled)
uName = inputVal2; // inputVal2: unknown → error: unknown is not assignable to type string

Hovering the error reads “unknown is not assignable to type string” — while switching the source to any clears it, since any disables type checking.

Narrowing unknown with typeof before assignment

unknown is more restrictive than any: first check what type of value is currently assigned before assigning it onward.

if (typeof inputVal === "string") {
	uName = inputVal; // safe: narrowed to string
}
console.log(uName);
console.log(typeof inputVal);

Live demo, take one: inputVal holds 100, so typeof is "number", the guard skips, and logging shows the initial name plus "number". Take two: after inputVal = "hello world", the guard passes and the log shows “hello world”

Why unknown beats any here

unknown beats any when the initial stored type is unknowable but the eventual use is known. If the type were known in advance, a concrete annotation (string, number, boolean) would do — unknown is for when it is not, and unlike any it keeps type safety by forcing the compatibility check before assignment.

course slide over the editor (“A Complete

Transcript corrections applied above: inputVal (the demo variable) (not input well); uName (username variable) (not uname, unam, unit, un name); it is logging “hello world” (not it is locking. Hello world); app.ts file (not sub.ts file); With an unknown type, first we need to check (not With a known type, first we need to check); typeof on inputVal (not type of one input well).

Prev: 050 — #16 Function Type for Callback | TypeScript Basics & Data Types | A Complete TypeScript Course · Next: 052 — #18 Never Type in TypeScript | TypeScript Basics & Data Types | A Complete TypeScript Course