TypeScript Basics
Learn the basics of Typescript, including data types, functions, classes, interfaces, aliases, and type checking.
We'll cover the following...
Data types
TypeScript has several basic data types. Let’s discuss some of them below:
Number: A number can be either an integer or a floating-point number.
String: Strings represent text data in TypeScript. They can be enclosed in single quotes (
') or double quotes ("). TypeScript supports various string operations and methods like concatenation, substring, etc.Boolean: Booleans represent logical values
trueorfalse. They are commonly used for conditional expressions and control flow.
Let’s discuss some other types of data types supported in TypeScript:
Array: Arrays in TypeScript are homogeneous collections of elements, meaning they can only contain elements of the same type.
Tuple: Tuples are arrays with a fixed number of elements, each potentially of a different type. They allow us to express an array where the type of a fixed number of elements is known but not necessarily all elements.
Enums: Enums are a way to give more friendly names to sets of numeric values. By default, enums begin numbering their members starting from 0, but we can also customize the start value or assign values explicitly.
nullorundefined: In TypeScript,nullandundefinedare subtypes of all other types, meaning we can assign them to variables of any type.void: Thevoidis used as the return type of functions that do not return a value. It is the absence of any type. Variables of typevoidcan only be assignedundefinedornull.
The any type allows us to opt out of type checking. It can represent any value and is useful when working with dynamic content or when migrating existing JavaScript code to TypeScript.
Functions
Functions in TypeScript can have parameters and return types annotated with types, allowing for type safety and better documentation.
TypeScript supports optional parameters, default parameters, rest parameters, and function overloading. Let’s discuss them in detail.
Optional parameters
Optional parameters are parameters that may or may not be provided when calling a function. They are denoted by adding a question mark (?) after the parameter name in the function declaration. If an optional parameter is not provided during the function call, its value becomes undefined.
Default parameters
The default parameter allows to specify default values for parameters in case they are not provided during the function call. Default parameters are indicated by assigning a value to the parameter in the function declaration.
Rest parameters
The rest parameter allows to represent an indefinite number of arguments as an array. They are denoted by prefixing the parameter name with three dots (...). Rest parameters gather the remaining arguments into an array.
Function overloading
Function overloading allows to define multiple function signatures for a single function name. TypeScript selects the appropriate function signature based on the number and types of arguments provided during the function call.
Classes
Classes in TypeScript allow to implement the object-oriented programming concepts such as inheritance,
Constructors: Constructors are special methods that are automatically called when a class instance is created.
Properties: Properties are variables that belong to a class and define the state or characteristics of the objects created from that class.
Methods: Methods are functions that are defined within a class and can perform actions or calculate values based on the object’s state.
Inheritance: Inheritance allows a class (subclass/derived class) to inherit properties and behavior (methods) from another class (superclass/base class).
Access modifiers: Access modifiers control the visibility and accessibility of class members (properties and methods).
public: Members are accessible from outside the class.private: Members are accessible only within the class that defines them.protected: Members are accessible within the class and its subclasses.
Interfaces
Interfaces define contracts in TypeScript, specifying the structure that an object must adhere to. They can include properties, methods, and
Type aliases
Type aliases allow to create custom names for types. They are especially useful for complex types like union types, intersection types, tuple types, and more. Type aliases enhance code readability and maintainability by providing descriptive names for types.
Type checking
TypeScript performs static type checking, which means type errors are caught at compile-time rather than runtime. It helps identify bugs early in the development process, improves code quality, and facilitates refactoring. TypeScript’s type system is powerful and expressive, supporting type inference, type assertions, type guards, and more.
We can see the error in the playground above that we can’t assign the number value to a string variable.