How many types of typescript it had ?
TypeScript has several types that can be categorized into primitive types, complex types, and others. Here's a breakdown:
1. Primitive Types
string
: Represents a sequence of characters (e.g.,"hello"
,'world'
).number
: Represents both integer and floating-point numbers (e.g.,42
,3.14
).boolean
: Represents a true/false value (e.g.,true
,false
).null
: Represents the intentional absence of any object value.undefined
: Represents a variable that has been declared but not assigned any value.symbol
: Represents a unique identifier.bigint
: Represents large integers (introduced in TypeScript 3.2).
2. Complex Types
array
: A collection of values, which can be of any type (e.g.,number[]
,Array<string>
).tuple
: A fixed-size array with elements of different types (e.g.,[string, number]
).object
: A non-primitive type that can be a collection of properties and methods (e.g.,{ name: string, age: number }
).
3. Other Types
any
: A type that allows any kind of value, effectively disabling type-checking (e.g.,let x: any = 42
).unknown
: Similar toany
, but safer because you can't perform operations on it until it's narrowed down (e.g.,let y: unknown = 42
).void
: Represents the absence of a value, often used for functions that don't return anything (e.g.,function foo(): void {}
).never
: Represents a value that never occurs, typically used for functions that throw errors or have infinite loops (e.g.,function error(): never { throw new Error("error"); }
).object
: A type for non-primitive values (objects, arrays, etc.), exceptnull
.
4. Specialized Types
literal types
: Specific values as types (e.g.,let color: 'red' | 'green' | 'blue';
).union types
: A type that can be one of multiple types (e.g.,let value: string | number;
).intersection types
: Combines multiple types into one (e.g.,type Employee = Person & Worker;
).type alias
: Creates a new name for a type (e.g.,type Point = { x: number, y: number };
).
5. Generics
generics
: Types that are defined with placeholders (e.g.,function identity<T>(arg: T): T { return arg; }
).
6. Enums
enum
: A way to define a set of named constants (e.g.,enum Color { Red, Green, Blue };
).
These are the core types and concepts in TypeScript. Some of them can be combined or used in advanced type manipulations to create powerful type systems in applications.