In this section, we will cover the basic types available in TypeScript. Understanding these types is fundamental to writing type-safe code. TypeScript provides several built-in types that help you define the shape and behavior of your data.
Key Concepts
-
Primitive Types: These are the most basic types in TypeScript.
number
string
boolean
null
undefined
symbol
bigint
-
Array Types: TypeScript allows you to define arrays of specific types.
-
Tuple Types: Tuples are arrays with a fixed number of elements where each element may be of a different type.
-
Enum Types: Enums allow you to define a set of named constants.
-
Any Type: A type that can hold any value.
-
Void Type: Represents the absence of any type, commonly used for functions that do not return a value.
-
Never Type: Represents the type of values that never occur.
Primitive Types
Number
TypeScript supports all number types, including integers and floating-point numbers.
String
Strings are used to represent text data.
Boolean
Booleans represent logical values: true
or false
.
Null and Undefined
null
and undefined
are types that have only one value each: null
and undefined
.
Symbol
Symbols are unique and immutable primitive values.
BigInt
BigInt is used for arbitrarily large integers.
Array Types
Arrays can be defined to hold elements of a specific type.
Alternatively, you can use the generic array type:
let numbers: Array<number> = [1, 2, 3, 4, 5]; let names: Array<string> = ["Alice", "Bob", "Charlie"];
Tuple Types
Tuples allow you to define an array with a fixed number of elements of specific types.
Enum Types
Enums allow you to define a set of named constants.
Any Type
The any
type can hold any value. It is useful when you don't know the type of a value in advance.
Void Type
The void
type is used for functions that do not return a value.
Never Type
The never
type represents values that never occur. It is often used for functions that always throw an error or never return.
Practical Exercises
Exercise 1: Define Basic Types
Define variables with the following types: number
, string
, boolean
, null
, undefined
, symbol
, and bigint
.
let age: number = 30; let name: string = "Alice"; let isStudent: boolean = true; let empty: null = null; let notAssigned: undefined = undefined; let uniqueId: symbol = Symbol("id"); let largeNumber: bigint = 12345678901234567890n;
Exercise 2: Define an Array and a Tuple
Define an array of numbers and a tuple with a string and a number.
Exercise 3: Use Enums
Define an enum for days of the week and use it to set a variable.
enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } let today: Day = Day.Wednesday;
Summary
In this section, we covered the basic types available in TypeScript, including primitive types, arrays, tuples, enums, and special types like any
, void
, and never
. Understanding these types is crucial for writing type-safe code in TypeScript. In the next section, we will explore type annotations and how to use them effectively in your code.
TypeScript Course
Module 1: Introduction to TypeScript
- What is TypeScript?
- Setting Up the TypeScript Environment
- Basic Types
- Type Annotations
- Compiling TypeScript
Module 2: Working with Types
Module 3: Advanced Types
Module 4: Functions and Modules
Module 5: Asynchronous Programming
Module 6: Tooling and Best Practices
- Linting and Formatting
- Testing TypeScript Code
- TypeScript with Webpack
- TypeScript with React
- Best Practices