MediumType System📖 Theory Question

What are literal types and how are they used for precise typing?

💡

Hint

A literal type narrows a string/number/boolean to a specific value — combines with union for exhaustive enumerations

Full Answer

Literal types narrow a broad primitive type to a specific value. "circle" is a subtype of string, 42 is a subtype of number.

// String literals
type Direction = 'north' | 'south' | 'east' | 'west';
function move(dir: Direction) { /* only 4 valid inputs */ }

move('north'); // ✅
move('up');    // ❌ Error

// Numeric literals
type DiceRoll = 1 | 2 | 3 | 4 | 5 | 6;

// Boolean literals — useful for discriminated unions
type Result<T> =
  | { success: true;  value: T }
  | { success: false; error: string };

function process(r: Result<number>) {
  if (r.success) console.log(r.value); // narrows via boolean literal
  else console.log(r.error);
}

const assertions — widen prevention:

// Without const — inferred as string
const dir = 'north'; // type: string (widened)

// With const — inferred as literal type
const dir2 = 'north' as const; // type: "north"

// Object with const — all properties become readonly literals
const config = { host: 'localhost', port: 3000 } as const;
// type: { readonly host: "localhost"; readonly port: 3000 }

// Array with const — tuple instead of array
const tuple = [1, 'a'] as const; // type: readonly [1, "a"]
💡 as const is the modern replacement for enums in many cases. It's a plain JavaScript value (no runtime overhead) while providing the exhaustive type checking of an enum.

More Type System Questions

EasyWhat is type inference in TypeScript and when does it kick in?EasyWhat is the difference between any, unknown, and never in TypeScript?EasyWhat are type assertions and when should you use them?EasyWhat is structural typing (duck typing) in TypeScript?

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint