TypeScript 2.0: Strict Null Checking and Compiler Type Guarding

Preventing runtime bugs. We explore strict null checks, tagged unions, and non-nullable types.

VP
SHIVAM ITCS
·14 July 2016·10 min read·1 views

Technical Overview & Strategic Context

While early TypeScript versions added types to JavaScript, they did not address a common source of runtime bugs: null and undefined references. In early TypeScript, a variable typed as String could still hold a null value, leading to runtime exceptions. The release of TypeScript 2.0 in mid-2016 resolved this by introducing Strict Null Checking (--strictNullChecks). This compiler flag changes how the type checker handles null and undefined, requiring developers to declare nullable fields explicitly.

Architectural Principle: Always enable strict null checks in TypeScript. Requiring explicit nullable declarations helps prevent null reference bugs at compile time.

Core Concepts & Architectural Blueprint

When --strictNullChecks is enabled, null and undefined are treated as distinct types. A variable declared as string cannot hold a null value, forcing developers to declare it as string | null. TypeScript 2.0 also introduces tagged union types, allowing the compiler to narrow types automatically inside switch statements.

Performance & Capability Comparison

Compiler ConfigurationNull Type BehaviorType SafetyDeveloper Action
Pre-TypeScript 2.0 DefaultNull assigned to any typeLow type safety for null checksRequires manual null safety checks
TypeScript 2.0 (Strict)Null is a distinct typeHigh type safety for null checksRequires explicit nullable declarations

Implementation & Code Pattern

To configure a TypeScript 2.0 project with strict type checking, follow these compiler steps:

  • Enable the strictNullChecks compiler flag inside the tsconfig.json file.
  • Declare variables that can hold null using union types (e.g. User | null).
  • Use type guards to verify variable types before accessing properties.
  • Define type definitions using @types scoped npm packages.
typescriptcode
// TypeScript 2.0 Strict Null Checks and Tagged Unions
interface ActiveUser {
    kind: "active";
    id: number;
    username: string;
}

interface GuestUser {
    kind: "guest";
    sessionToken: string;
}

// Tagged Union type definition
type AppUser = ActiveUser | GuestUser;

function handleUser(user: AppUser | null) {
    // Compiler blocks unsafe access if strictNullChecks is active
    // console.log(user.kind);
    
    if (user === null) {
        console.log("No user session active.");
        return;
    }

    // Type guard narrows type inside switch statement
    switch (user.kind) {
        case "active":
            console.log(`Welcome back, ${user.username}`);
            break;
        case "guest":
            console.log("Guest login session active.");
            break;
    }
}

Operational Governance & Future Outlook

TypeScript 2.0's strict null checking and type narrowing features improved type safety in JavaScript applications. Enforcing these checks at compile time helps teams find bugs early, improving system reliability.

VP
Vijay Paliwal
Founder, SHIVAM ITCS · 18+ years enterprise & AI engineering
MCA · Ex-HiveGPT USA · Ex-Social27 Seattle
TypeScript 2.0: Strict Null Checking and Compiler Type Guarding | SHIVAM ITCS Blog | SHIVAM ITCS