Understanding TypeScript Primitives and Type Signatures
TypeScript extends JavaScript by introducing a static type system that validates data structures during compilation. This foundation relies on primitive definitions, explicit signatures, and automatic inference mechanisms.
Core Primitive Definitions
Numeric values encompass both integers and floating-point decimals. String literals handle textual sequences, while booleans strictly evaluate to logical states.
let processorCores: number = 8;
let deviceModel: string = "Alpha-X1";
let isOnline: boolean = true;
Empty states map to null and undefined. Strict compiler configurations treat these as distinct types that must be handled explicit rather than implicitly coalesced.
let cachedResponse: null = null;
let uninitializedConfig: undefined = undefined;
Managing Unpredictable Data
The any type disables type checking, allowing unrestricted reassignment and method invocation. This bypasses compiler validation entirely.
let legacyPayload: any = 42;
legacyPayload = { id: 99, active: false };
legacyPayload.callMethod(); // Compiles despite potential runtime failure
Conversely, unknown accepts any value but enforces type narrowing before operations. This maintains safety when processing external inputs.
let externalData: unknown = "initial string";
externalData = 100;
let parsedValue: string;
if (typeof externalData === "string") {
parsedValue = externalData.toUpperCase(); // Safe execution path
}
// Direct assignment fails without explicit verification
// parsedValue = externalData;
Adopting unknown for ambiguous sources preserves the compiler's ability to catch invalid operations.
Structured Collections and Constants
Arrays enforce homogeneity, requiring all elements to share a single type declaration.
let sensorReadings: number[] = [12.5, 14.8, 13.2];
// Alternative generic syntax: Array<number>
Tuples define fixed-length sequences where each position holds a predetermined type, useful for coordinate mapping or key-value pairs.
let coordinatePair: [string, number] = ["Latitude", 45.90];
Enumerations organize named numeric constants under a unified namespace. Members auto-increment from zero unless explicit initialized.
enum HttpStatusCode {
Success = 200,
BadRequest = 400,
ServerError = 500
}
let responseStatus: HttpStatusCode = HttpStatusCode.Success;
Signature Enforcement and Automatic Inference
Explicit annotations apply to variable declarations, function parameters, and return values to establish strict contracts.
function calculateArea(radius: number, pi: number = 3.14159): number {
return pi * Math.pow(radius, 2);
}
let circleArea: number = calculateArea(5);
// let invalidArea: number = calculateArea("five"); // Compilation fails
When an initial assignment is present, the compiler deduces the type automatically, eliminating redundant syntax while preserving strict checks.
let autoDetectedCount = 10; // Inferred as number
let autoDetectedLabel = "sensor"; // Inferred as string
let autoDetectedFlag = false; // Inferred as boolean
// Reassignment outside the inferred domain triggers an error
// autoDetectedCount = "ten";
The compiler validates these inferred assignments during compilation, ensuring type consistency across the codebase.