Translate

Friday, 16 February 2024

What are Built in/Primitive & User Defined/Non-primitive types in Typescript?59

  What are Built in/Primitive & User Defined/Non-primitive types in Typescript?


In TypeScript, data types are broadly categorized into two groups: primitive and non-primitive. Understanding these distinctions is crucial for effectively using type annotations and ensuring type safety in your code.

1. Primitive Types:

  • Built-in/Predefined: These are fundamental data types directly supported by the JavaScript engine and TypeScript. They are immutable, meaning their values cannot be changed once assigned.

  • Types: There are seven primitive types in TypeScript:

  • string: Represents textual data (e.g., "Hello world").

  • number: Represents numeric values (e.g., 42, 3.14).

  • boolean: Represents true or false values (e.g., true, false).

  • symbol: Represents unique, immutable values used for object properties (e.g., Symbol("unique")).

  • null: Represents the absence of a value (e.g., null).

  • undefined: Represents a variable that has not been declared or assigned a value (e.g., let myVar;).

  • bigint: Represents integers larger than the number type can hold (e.g., 1000000000000000n).

2. Non-primitive Types:

  • User-defined/Composite: These are not directly supported by JavaScript but can be created by developers using various mechanisms like objects, arrays, functions, and classes.

  • Mutability: They can be mutable, meaning their values can be changed after creation.

  • Examples: Common non-primitive types include:

  • Object: A collection of key-value pairs.

  • Array: An ordered collection of elements of any type.

  • Function: A block of code that can be executed.

  • Class: A blueprint for creating objects with properties and methods.

  • Enum: A set of named constants of the same type.

  • Interfaces: Contracts defining the structure of objects.

Key Differences:

| Feature | Primitive Types | Non-primitive Types | |-------------------+-------------------------------------------------------------------|----------------------------------------------------------------- | | Definition | Built-in, fundamental data types | User-defined, composite data structures | | Mutability | Immutable (values cannot change after assignment) | Mutable (values can change after creation) | | Examples | string, number, boolean, symbol, null, undefined, bigint | Object, Array, Function, Class, Enum, Interface |

Choosing the Right Type:

Selecting the appropriate type depends on your data and desired behavior. Primitive types are suitable for simple values like numbers and strings. Non-primitive types offer more flexibility for complex data structures and object-oriented programming.

By understanding the distinctions between primitive and non-primitive types in TypeScript, you can make informed decisions about type annotations, enhancing the type safety, readability, and maintainability of your code.


No comments:

Post a Comment

Note: only a member of this blog may post a comment.