Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A type-annotated variable in TypeScript is an identifier explicitly bound to a specific data type using a colon (:) syntax. This explicit declaration instructs the TypeScript compiler to enforce static typing rules, ensuring that only values conforming to the specified type signature can be assigned to the variable during the compilation phase.

Syntax

The structural pattern for a type-annotated variable consists of the declaration keyword, the identifier, the colon token, the type annotation, and an optional initialization.
// General signature
<keyword> <identifier>: <TypeAnnotation> = <value>;

// Uninitialized declaration
let <identifier>: <TypeAnnotation>;

Technical Mechanics

  • Static Type Checking: The TypeScript compiler (TSC) uses the annotation to validate assignments and operations associated with the variable via the Abstract Syntax Tree (AST). If a value’s type does not satisfy the annotated type contract, the compiler emits a compile-time diagnostic error (e.g., TS2322: Type 'X' is not assignable to type 'Y').
  • Type Erasure: Type annotations are strictly compile-time constructs. During transpilation, the TypeScript compiler strips the colon and the type identifier from the emitted JavaScript code. The annotations have zero runtime footprint.
  • Overriding Inference: When a variable is initialized simultaneously with its declaration, TypeScript typically infers the type. Providing an explicit type annotation overrides the compiler’s default type inference algorithm, forcing the variable to adhere strictly to the developer-defined type constraint (e.g., widening or narrowing the type).

Annotation Categories

Type annotations can range from primitive data types to complex, composite structures. Primitives Annotations for fundamental JavaScript types.
let isActive: boolean = true;
const maxRetries: number = 5;
let endpointUrl: string = "https://api.example.com";
Arrays and Tuples Annotations defining collections, either by uniform type or fixed-length/fixed-type structures.
// Array syntax
let byteStream: number[] = [0, 1, 1, 0];
let stringBuffer: Array<string> = ["chunk1", "chunk2"];

// Tuple syntax (fixed length and types)
let coordinate: [number, number, string] = [10.5, 20.1, "z-axis"];
Object Literals Inline structural typing defining the exact shape, properties, and value types of an object.
let configuration: { 
    host: string; 
    port: number; 
    secure?: boolean; // Optional property
} = {
    host: "localhost",
    port: 8080
};
Unions and Intersections Annotations that combine multiple types into a single variable contract.
// Union: Variable can be one of multiple types
let processId: string | number = 1024;

// Intersection: Variable must satisfy multiple type contracts simultaneously
type HasId = { id: string };
type HasTimestamps = { createdAt: Date; updatedAt: Date };

let entity: HasId & HasTimestamps;
Function Signatures Annotating a variable that holds a function reference, defining both the parameter types and the return type.
let calculateHash: (payload: string, salt: number) => string;
Master TypeScript with Deep Grasping Methodology!Learn More