- TypeScript Tutorial
- TypeScript Home
- TypeScript Introduction
- TypeScript Setup
- TypeScript First Program
- TypeScript vs JavaScript
- TypeScript Data Types
- TypeScript Type Inference
- TypeScript Type Annotations
- TypeScript Interfaces
- TypeScript Enums
- TypeScript Type Aliases
- TypeScript Type Assertions
- TypeScript Variables
- TypeScript Functions
- TypeScript Functions
- TypeScript Optional Parameters
- TypeScript Default Parameters
- TypeScript Rest Parameters
- TypeScript Arrow Functions
- Classes and Objects
- Introduction to Classes
- Properties and Methods
- Access Modifiers
- Static Members
- Inheritance
- Abstract Classes
- Interfaces vs Classes
- Advanced Types
- TypeScript Union Types
- TypeScript Intersection Types
- TypeScript Literal Types
- TypeScript Nullable Types
- TypeScript Type Guards
- TypeScript Discriminated Unions
- TypeScript Index Signatures
- TypeScript Generics
- Introduction to Generics
- TypeScript Generic Functions
- TypeScript Generic Classes
- TypeScript Generic Constraints
- TypeScript Modules
- Introduction to Modules
- TypeScript Import and Export
- TypeScript Default Exports
- TypeScript Namespace
- Decorators
- Introduction to Decorators
- TypeScript Class Decorators
- TypeScript Method Decorators
- TypeScript Property Decorators
- TypeScript Parameter Decorators
- Configuration
- TypeScript tsconfig.json File
- TypeScript Compiler Options
- TypeScript Strict Mode
- TypeScript Watch Mode
TypeScript Data Types
TypeScript provides a variety of data types to ensure type safety and improve code quality. These data types allow developers to define the kind of data that variables, function parameters, and return values will hold.
Basic Data Types
Number
The number
type is used for numeric values, including integers and floating-point numbers.
String
The string
type is used for text data, enclosed in single ('
) or double ("
) quotes.
Boolean
The boolean
type represents true
or false
.
Special Data Types
Any
The any
type allows a variable to hold any type of data. It bypasses type-checking.
Unknown
The unknown
type is safer than any
because it enforces type checking before operations.
Null and Undefined
The null
and undefined
types are subtypes of all other types.
Advanced Data Types
Array
Arrays in TypeScript can store multiple values of the same type.
Tuple
A tuple
is an array with fixed types and a specific number of elements.
Enum
Enum
allows defining a set of named constants.
Object
The object
type represents non-primitive types like objects.
Union
The union
type allows a variable to hold more than one type.
Void
The void
type is used for functions that do not return any value.
Never
The never
type represents a value that never occurs, often used for errors or infinite loops.
Summary
TypeScript provides a rich set of data types, including basic types like number
, string
, and boolean
, as well as advanced types like tuple
, enum
, and union
. Using these types ensures better type safety and improves code maintainability in TypeScript applications.