- In TypeScript, primitive types are the most basic data types, and they are the building blocks for handling data. They correspond to simple values that are immutable (their actual value can't be changed once created). TypeScript has several primitive types that are based on JavaScript’s primitive types but with the added benefit of static typing. Here’s a breakdown of the most common primitive types:
- number: The number type is used for both integers and floating-point values. TypeScript doesn’t distinguish between integer and floating-point numbers; all numbers are of type number.
// Example:
let age: number = 25;
let price: number = 99.99;
- Key Points: It supports decimal, hexadecimal, octal, and binary literals.
let decimal: number = 10;
let hex: number = 0xf00d;
let binary: number = 0b1010;
let octal: number = 0o744;
- string: The string type is used to represent textual data. TypeScript strings are enclosed in either single quotes ('...'), double quotes ("..."), or backticks (`...`) for template literals.
// Example:
let firstName: string = "John";
let greeting: string = Hello, ${firstName}!; // Template literal
- Key Points: Backticks ( ) are used for template literals, which allow embedding expressions and multi-line strings.
- boolean: The boolean type represents logical values that can be either true or false. This type is used to handle binary logic.
// Example:
let isCompleted: boolean = true;
let hasError: boolean = false;
- Key Points: Boolean values are useful for controlling program flow and conditions.
- null and undefined
- null: Represents the intentional absence of any object value.
- undefined: Denotes a variable that has been declared but not initialized with a value.
// Example:
let value: null = null;
let result: undefined = undefined;
- Key Points: Both null and undefined are valid types in TypeScript, though they are treated as subtypes of other types in strict mode.
- With the strictNullChecks flag enabled, null and undefined must be explicitly assigned and cannot be mixed with other types.
- symbol: The symbol type is used to create unique and immutable values, often used as object property keys to avoid name clashes.
// Example:
let uniqueId: symbol = Symbol("id");
- Key Points: Each symbol is unique, even if created with the same description.
- bigint: Introduced in ES2020, bigint is used for very large integers that exceed the number type’s limit. It allows you to perform calculations on arbitrarily large numbers.
// Example:
let bigNumber: bigint = 1234567890123456789012345678901234567890n;
- Key Points:
- A bigint value is denoted with an n suffix, such as 123n.
- Useful when working with large integers (e.g., cryptography, scientific calculations).
- TypeScript enforces static type checking, which means the type of each variable must be explicitly or implicitly known at compile time. This reduces the chances of errors at runtime.
// Example of Type Error:
let age: number = 25;
age = "twenty-five"; // Error: Type 'string' is not assignable to type 'number'
- Type Inference: TypeScript can often infer the type of a variable based on the value assigned to it. If you assign a number to a variable without an explicit type annotation, TypeScript will automatically infer it as a number.
// Example:
let isReady = true; // TypeScript infers the type as 'boolean'
- Even though type annotations provide more clarity, you can rely on TypeScript’s inference system when the type is obvious.
- undefined: A variable is declared but has no value assigned to it.
- null: A variable is explicitly set to have no value.
// Example:
let a: undefined = undefined;
let b: null = null;
- In TypeScript’s strict mode (strictNullChecks), you have to explicitly type variables that may be null or undefined. Without strict mode, both null and undefined can be assigned to other types like string or number.
- TypeScript offers primitive types that are based on JavaScript's core types.
- The use of types (number, string, boolean, etc.) helps avoid bugs related to incorrect data types and improves code readability.
- Type inference is helpful but it’s always a good practice to add type annotations for clarity in complex scenarios.
- Strict typing with null and undefined ensures that unintentional nullish values do not cause runtime errors, especially in large codebases.
- The primitive types in TypeScript provide a solid foundation for building type-safe applications. By understanding and using these types, developers can write more predictable, error-free code, as TypeScript’s static type system catches mistakes during development instead of at runtime.
No comments:
Post a Comment