Skip to main content

Data Types & Variables

Ayesha
EditReport

Mastering data types is essential for maximizing TypeScript's type-safety assertions. Let's look at primitive types, variables, and type safety constraints.

Video Explanation​

1. Variable Declarations (let vs const)​

TypeScript strictly enforces standard JavaScript scoping layouts:

  • let: Declares a block-scoped mutable variable.
  • const: Declares a block-scoped immutable variable assignment contract.
let balance: number = 5000;
balance = 4500; // Allowed

const secretKey: string = "AX9842";
// secretKey = "NEW"; // Error: Cannot assign to 'secretKey' because it is a constant.

2. Primitive Types​

TypeScript features several foundational primitive built-in data types:

const isComplete: boolean = true;
const processingRatio: number = 0.742; // Handles all integers, floats, hex, binary
const developerName: string = "Trushi";

const unassigned: undefined = undefined;
const emptyReference: null = null;

3. Special Utility Types​

TypeScript introduces escape hatches and fallback types for strict evaluation environments:

a. Any​

The any type disables compile-time static safety checking. Use it carefully:

let untrackedPayload: any = "Initial text string";
untrackedPayload = 42; // Allowed without compile warnings

b. Unknown​

The safe counterpart to any. You can assign anything to an unknown variable, but you cannot perform operations on it without performing dynamic type narrowing or validation checks first:

let safeInput: unknown = "Dynamic data stream";

// Cannot use safely directly:
// let len = safeInput.length; // Error

if (typeof safeInput === "string") {
let len = safeInput.length; // Allowed! Type narrowed safely to string
}

c. Void & Never​

void: Represents the total absence of a return value (commonly used for function outcomes).

never: Represents an unachievable application execution branch state (e.g., functions that throw errors indefinitely or process infinite loops).

function throwFatalError(err: string): never {
throw new Error(err);
}

4. Type Assertions (Casting)​

When working with loose structural inputs, you can explicitly override type assumptions using type assertions:

let standardData: unknown = "Strict processing contract";

// Angle-bracket syntax (Not usable in JSX files)
let sizeA: number = (<string>standardData).length;

// 'as' Keyword syntax (Recommended)
let sizeB: number = (standardData as string).length;
Track Your Progress

Done with this topic? Mark it as complete to track your progress.

Was this page helpful?

💬

Discuss this page

Have a question or spot something confusing in "Data Types & Variables"? Ask below. Backed by GitHub Discussions—maintainers receive system notifications directly.