QUICK START:HTMLCSSJavaScriptTypeScriptPythonSQLReactNext.jsNode.jsDSASystem DesignDevOpsCybersecurityAI / ML
Intermediate 16 min readModule: Module 2: Basic Types, Tuples & Enums

Tuples, Enums & Unknown vs Any

Master fixed-length array tuples, numeric/string enums, and the type-safe 'unknown' primitive.

What You Will Learn in This Lesson

  • Tuples with fixed length and explicit element types
  • String literal unions vs TypeScript enums
  • Why 'unknown' is 100% safer than 'any'

Introduction & Core Concept

TypeScript extends JavaScript primitives with strict types like Tuples (fixed-size array models) and the 'unknown' top type.
WHY DOES THIS MATTER IN THE REAL WORLD?

Unlike 'any' which completely turns off type checking, 'unknown' forces developers to verify the type before accessing properties.

Type-Safe Unknown Handling

typescript
typescript
1
2
3
4
5
6
7
function handleData(input: unknown) {
if (typeof input === "string") {
console.log("String Length:", input.length); // Safe!
}
}
handleData("KWAS Academy");

Line-by-Line Technical Breakdown

1Always prefer string literal unions ('admin' | 'user') over numeric enums in modern TypeScript.

Try It Yourself (Interactive Editor)

Modify the code in real-time and click Run to test live browser output and console logs.

Intelligent Code Runner & Live Sandbox[TYPESCRIPT]
TYPESCRIPT SOURCE EDITOR
Interactive Live Code

Industry Best Practices & Professional Standards

  • Never use 'any'; use 'unknown' when dealing with dynamic external API responses.

Lesson Summary & Core Takeaways

  • Strict types eliminate unexpected runtime type errors.