DataTypes in TypeScript

  • TypeScript supports several built-in data types, which are similar to those found in JavaScript.
Here are the primary data types in TypeScript:
  • Boolean: Represents a logical value of either `true` or `false`.
  • Number: Represents numeric values, including both integer and floating-point numbers. TypeScript supports decimal, hexadecimal, octal, and binary number literals.
  • String: Represents a sequence of characters. Strings can be enclosed in single quotes ( ' ), double quotes ( " ), or backticks ( ` ).
  • Array: Represents an ordered collection of values of a specific type. TypeScript provides array types using square brackets (`[]`) notation, such as `number[]` for an array of numbers or `string[]` for an array of strings. Arrays can also be written using the generic `Array<T>` notation.
  • Tuple: A tuple is an array-like structure that allows storing a fixed number of elements of different types. The types of individual elements in a tuple are known and fixed at compile-time.
  • Enum: Enumerated types allow defining a set of named constant values. Each constant in the enum has an associated numeric value, and TypeScript provides several options for working with enum values.
  • Any: The `any` type represents a dynamic or untyped value. It allows variables to hold values of any type and disables static type checking for those variables. Using `any` should be avoided when possible, as it bypasses type checking and the benefits of TypeScript's static typing.
  • Void: The `void` type indicates the absence of any type. It is commonly used as the return type of functions that do not return a value.
  • Null and Undefined: TypeScript has `null` and `undefined` as separate types that represent the absence of a value. `null` is a value assigned to a variable to indicate the intentional absence of an object, while `undefined` typically represents an uninitialized variable.
  • Object: Represents a non-primitive type, i.e., anything that is not of the types `number`, `string`, `boolean`, `symbol`, `null`, or `undefined`. It is the type used for variables that can hold instances of classes or other complex objects.
  • These are the fundamental data types in TypeScript. TypeScript also provides advanced types such as union types, intersection types, literal types, and more, which enable more expressive and precise type definitions.

No comments:

Post a Comment