0 day streak
Code library

Beginner TypeScript exercises

Read these Beginner TypeScript snippets line by line — each one comes with a written breakdown of what the code does and why.

TypeScript · Beginner

Simple Interfaces

Interfaces are used to describe the shape of an object. They act as a contract for what properties an object should have.

SyntaxBest Practices
Open snippet →
TypeScript · Beginner

Type Annotations

Type annotations allow us to explicitly specify the type of a variable. This helps the compiler catch errors if we try to assign a value of the wrong type.

SyntaxData Types
Open snippet →
TypeScript · Beginner

Basic Function Types

In TypeScript, you can define the types of the parameters a function receives and the type of the value it returns.

FunctionsSyntax
Open snippet →
TypeScript · Beginner

Defining Arrays

TypeScript allows you to define arrays that contain only a specific type of element, preventing accidental mixed-type arrays.

Arrays & ListsData Types
Open snippet →
TypeScript · Beginner

Union Types

Union types allow a variable to be one of several types. This is useful when a value could legitimately be different types.

Data TypesSyntax
Open snippet →
TypeScript · Beginner

Optional Chaining

The optional chaining operator (?.) permits reading the value of a property deep within a chain of objects without having to check if each reference is valid.

SyntaxBest Practices
Open snippet →
TypeScript · Beginner

Nullish Coalescing

The nullish coalescing operator (??) returns its right-hand operand when its left-hand operand is null or undefined. Unlike ||, it treats 0 as a valid value.

SyntaxControl Flow
Open snippet →
TypeScript · Beginner

Type Assertions

Type assertions are a way to tell the compiler to treat a value as a specific type when you have more information about it than TypeScript does.

Syntax
Open snippet →
TypeScript · Beginner

Numeric Enums

Enums allow a developer to define a set of named constants, making it easier to document intent or create a set of distinct cases.

SyntaxData Types
Open snippet →
TypeScript · Beginner

Readonly Modifier

The 'readonly' keyword makes a property immutable. It can only be assigned a value at declaration or within the constructor of the class.

SyntaxOOP
Open snippet →
TypeScript · Beginner

Basic Type Annotations

Type annotations allow you to explicitly state what type of data a variable should hold, helping catch errors during development.

SyntaxData Types
Open snippet →
TypeScript · Beginner

Array Type Syntax

You can define an array that only allows a specific type of element by adding square brackets after the type name.

Arrays & ListsData Types
Open snippet →
TypeScript · Beginner

Tuple Types

A tuple is a specialized array where you know exactly how many elements it contains and which types are at specific positions.

SyntaxData Types
Open snippet →
TypeScript · Beginner

Readonly Properties

The 'readonly' modifier makes a property immutable after the object is first initialized, preventing accidental changes to sensitive data.

SyntaxBest Practices
Open snippet →
TypeScript · Beginner

The 'unknown' Type

The 'unknown' type is a safer version of 'any'. You cannot perform operations on an 'unknown' value until you verify its type using a type check.

Data TypesBest Practices
Open snippet →
TypeScript · Beginner

Type Aliases for Objects

Type aliases allow you to give a custom name to an object structure, making it easy to reuse the same shape across your code without using interfaces.

SyntaxData Types
Open snippet →
TypeScript · Beginner

Optional Chaining

Optional chaining allows you to safely access properties that may be null or undefined. If any part of the chain is missing, it returns undefined instead of throwing a runtime error.

SyntaxBest Practices
Open snippet →
TypeScript · Beginner

Nullish Coalescing Operator

The nullish coalescing operator (??) returns the right-hand value only if the left-hand value is null or undefined. Unlike the OR (||) operator, it doesn't trigger for empty strings or 0.

SyntaxBest Practices
Open snippet →
TypeScript · Beginner

The 'void' Return Type

In TypeScript, 'void' is used to indicate that a function does not return any value. It helps prevent logic errors where a return value might be expected but doesn't exist.

FunctionsData Types
Open snippet →
TypeScript · Beginner

String Enums

String enums allow you to define a set of named constants with string values. They provide better readability and easier debugging compared to numeric enums because the values are descriptive.

Data Types
Open snippet →
TypeScript · Beginner

The 'never' Type

The 'never' type represents values that never occur. It is used for functions that always throw an error or contain infinite loops, meaning they never reach a return statement.

FunctionsError Handling
Open snippet →
TypeScript · Beginner

Literal Types

Literal types allow you to specify the exact value a string, number, or boolean must have, rather than just any value of that type.

SyntaxData Types
Open snippet →
TypeScript · Beginner

Function Overloads

Function overloads allow you to define multiple ways a function can be called, providing better type checking for different input combinations.

SyntaxFunctions
Open snippet →
TypeScript · Beginner

Array Type Annotations

You can specify the type of elements allowed in an array using the square bracket syntax (type[]) after the type name.

Arrays & ListsData Types
Open snippet →