TypeScript Exercises
Fill in the blanks to check your knowledge. Each exercise belongs to a lesson.
TypeScript Introduction
Exercise
Add a type to a variable.
const name string = "Ada";
TypeScript Get Started
Exercise
Compile a file.
hello.tstsconfig.json
Exercise
Create a starter config.
tsc TypeScript Syntax
Exercise
Annotate with this symbol.
let n number = 1;
Variables: let and const
Exercise
Constant keyword.
pi = 3.14;Type Inference
Exercise
Annotate a parameter.
function f(x number) {}
Numbers and Strings
Exercise
Insert a value in a template.
`Hi name}`
any, unknown and never
Exercise
Safe alternative to any.
let x: = 1;
null, undefined and Strict Null Checks
Exercise
Default when null or undefined.
name "guest"
Type Assertions
Exercise
Assert a type.
value string
TypeScript Operators
Exercise
Strict equality.
a b
Objects and Destructuring
Exercise
Copy with spread.
const c = { user };
Typed Array Methods
Exercise
Combine to one value.
n.((a, b) => a + b, 0)
Basic Types
Exercise
Which type is safer than any?
let x: = 5;
Arrays and Tuples
Exercise
Declare an array of numbers.
const n: [] = [1, 2];
Objects, Interfaces and Type Aliases
Exercise
Mark a property optional.
interface A { email: string }
Unions and Literal Types
Exercise
Finish the union.
type Id = string | ;
Enums
Exercise
Declare an enum.
Color { Red, Blue }Literal Types and const Assertions
Exercise
Freeze with literal types.
const c = { a: 1 } const;
Intersection Types
Exercise
Combine two types.
type P = A B;
Type Aliases vs Interfaces
Exercise
Merge declarations.
User { id: number }Index Signatures and Records
Exercise
Object with string keys and number values.
Record<string, >
Readonly and Named Tuples
Exercise
Prevent changes.
const a: number[] = [1];
Basic Types
Exercise
Annotate a number.
let n: = 5;
Type Inference
Exercise
Hover shows the type.
let n = ;
Arrays and Tuples
Exercise
Array of strings.
const a: [] = ["x"];
any, unknown and never
Exercise
Safe unknown type.
let v: = 1;
Union Types
Exercise
Either type.
let x: | number;
Literal Types
Exercise
Only allow "on" or "off".
type S = "on" | ;
Type Aliases
Exercise
Name a type.
ID = number;Interfaces
Exercise
Optional property.
email: string
Extending Interfaces
Exercise
Inherit shape.
interface B A {}
readonly and Optional Properties
Exercise
Cannot change.
host: stringEnums
Exercise
Declare an enum.
Level { A, B }Type Assertions
Exercise
Assert a type.
(v string)
Type Narrowing
Exercise
Check a type.
if ( x === "string")
Discriminated Unions
Exercise
Discriminant name.
type A = { : "a" }
null and undefined Checks
Exercise
Default value.
s "none"
void and never
Exercise
No return value.
function f(): {}
Index Signatures
Exercise
Any string key.
{ [k: ]: number }
Record
Exercise
Key and value.
<string, number>Practice: Typed Event Emitter
Exercise
Key type.
K extends Events
Type Narrowing and Guards
Exercise
Return type of a type guard.
function f(x: unknown): x string
Exhaustive Checks
Exercise
Impossible value type.
const x: = l;
Loops and Iteration
Exercise
Loop over values.
for (const n nums)
Functions
Exercise
Type a return value.
function f(): { return 1; }
Generics
Exercise
Declare a type parameter.
function idT>(x: T): T { return x; }
Utility Types
Exercise
Make all properties optional.
type D = <User>;
Optional, Default and Rest Parameters
Exercise
Collect extra arguments.
function f(nums: number[]) {}
Function Types and Overloads
Exercise
Arrow in a function type.
type F = (a: number) string
this, void and Callbacks
Exercise
Return nothing.
function f(): {}
Generic Constraints and Defaults
Exercise
Property names of T.
K extends T
Generic Classes and Interfaces
Exercise
Declare a generic class.
class StackT> {}
Function Types
Exercise
Return type.
function f(): { return 1; }
Optional and Default Parameters
Exercise
Optional param.
function f(a: number) {}
Rest Parameters
Exercise
Rest syntax.
function f(a: number[]) {}
Function Overloads
Exercise
Overload count.
Overloads share one Generics
Exercise
Type parameter.
function id<>(x: T) { return x; }
Generic Constraints
Exercise
Constrain T.
<T { id: number }>
Generic Interfaces and Types
Exercise
Type parameter list.
interface Box { v: T }
keyof
Exercise
Keys of a type.
Usertypeof in Types
Exercise
Type of a value.
type T = value
Classes
Exercise
Hide a member from outside.
score: number;Async, Promises and Modules
Exercise
Wait for a promise.
const v = fetchScore(1);
Class Modifiers
Exercise
Truly private field marker.
balance = 0;Inheritance and Abstract Classes
Exercise
Parent class call.
.speak()Implementing Interfaces
Exercise
Promise an interface.
class D Printable
ES Modules and Imports
Exercise
Import only a type.
import { User } from "./t";
Declaration Files and Globals
Exercise
Install types for a library.
npm i -D @types/Classes
Exercise
Parameter property.
constructor( name: string)
public, private and protected
Exercise
Class only.
secret = 1implements
Exercise
Follow an interface.
class A I {}
Abstract Classes
Exercise
Cannot instantiate.
class A {}Static and Accessors
Exercise
Class property.
count = 0Modules: import and export
Exercise
Export a value.
const x = 1;Decorators Overview
Exercise
Apply decorator.
loggedMapped Types
Premium exercise. See plans
Conditional Types and infer
Premium exercise. See plans
Template Literal Types
Premium exercise. See plans
More Utility Types
Premium exercise. See plans
Decorators and Symbols
Premium exercise. See plans
Promises and async/await Types
Premium exercise. See plans
Error Handling and Result Types
Premium exercise. See plans
JSON and Runtime Validation
Premium exercise. See plans
TypeScript with the DOM
Premium exercise. See plans
Testing TypeScript
Premium exercise. See plans
Linting, Formatting and Build Tools
Premium exercise. See plans
Typed Express APIs
Premium exercise. See plans
TypeScript with React
Premium exercise. See plans
Project References and Monorepos
Premium exercise. See plans
Migrating JavaScript to TypeScript
Premium exercise. See plans
TypeScript Best Practices
Premium exercise. See plans
Partial, Pick and Omit
Exercise
Make optional.
<User>Readonly and Required
Exercise
All required.
<U>Mapped Types
Exercise
Loop over keys.
[K keyof T]
Conditional Types
Exercise
Ternary in types.
T extends U X : Y
Template Literal Types
Exercise
Template type.
type T = `a${}`
as const
Exercise
Freeze literals.
{ a: 1 } as Custom Type Guards
Exercise
Guard return.
function f(x: any): x string
satisfies
Exercise
Check keeping type.
const x = {} T
Inferring with infer
Exercise
Capture a type.
T extends ( U)[] ? U : T
Promises and async Types
Exercise
Return type.
async function f(): <number>
Typing Errors
Exercise
Check Error.
e Error
tsconfig.json
Exercise
Strict mode.
"": true
Useful Strict Flags
Exercise
Check unsafe index.
noUncheckedAccess
Using tsc
Exercise
Check only.
tsc --Using JavaScript Libraries
Exercise
Types package.
npm i -D @types/Generic Classes
Exercise
Class generic.
class Box {}
Immutable Updates
Exercise
Readonly array.
number[]Object Types and Excess Properties
Exercise
Shape matching.
TypeScript uses typing
Exhaustive Checks
Exercise
Impossible type.
const n: = x;
Practice: Typed Todo List
Exercise
Boolean field.
done: Practice: Generic Cache
Exercise
Non-null assertion.
this.m.get(k)Practice: API Result Type
Exercise
Tag value.
{ ok: ; value: T }
Practice Projects
Exercise
Practice puzzles repo.
type-