Welcome to a new chapter of our Jurassic Park adventure! Imagine the following situation: the park's programmers created an access control system for the dinosaur enclosures, designed to protect both guests and employees. Everything worked great until one moment...
1// access-control-system.js
2
3function grantAccess(employee, accessLevel) {
4 if (employee.permissions >= accessLevel) {
5 return true;
6 }
7 return false;
8}
9
10// Elsewhere in the system
11const drMalcolm = {
12 firstName: "Ian",
13 lastName: "Malcolm",
14 role: "Mathematician"
15 // oops... missing the 'permissions' field!
16};
17
18// Attempt to access the dinosaur enclosure
19const accessToT_Rex = grantAccess(drMalcolm, 5);
20console.log("Access to T-Rex enclosure:", accessToT_Rex); // false, but no error!
21
22// Later in the code...
23if (accessToT_Rex) {
24 open_gate_to_enclosure('t-rex'); // This operation will never execute
25} else {
26 console.log("Dr. Malcolm has no access"); // This will run, but nobody noticed the bug
27}Did you notice what went wrong? The
drMalcolm object was missing the permissions property, causing the system to incorrectly deny him access! In JavaScript this situation generated no error - the permissions property was simply undefined, which when compared to a number gave a result of false.Now imagine the consequences of this situation in a real dinosaur park:
This is exactly where TypeScript comes in handy.
TypeScript is a superset of JavaScript that adds optional static typing. It was created by Microsoft and is developed as an open source project.
Think of TypeScript as an advanced security system for Jurassic Park:
The same situation in TypeScript would look like this:
1// access-control-system.ts
2
3// We define an interface specifying what an employee object should look like
4interface Employee {
5 firstName: string;
6 lastName: string;
7 role: string;
8 permissions: number; // Required field!
9}
10
11function grantAccess(employee: Employee, accessLevel: number): boolean {
12 if (employee.permissions >= accessLevel) {
13 return true;
14 }
15 return false;
16}
17
18// Elsewhere in the system
19const drMalcolm = {
20 firstName: "Ian",
21 lastName: "Malcolm",
22 role: "Mathematician"
23 // TypeScript will catch the error: Property 'permissions' is missing in type
24 // '{ firstName: string; lastName: string; role: string; }' but required in type 'Employee'.
25};
26
27// This line will cause a compilation error - TypeScript will protect us!
28const accessToT_Rex = grantAccess(drMalcolm, 5);In the above example TypeScript will detect the error at the code-writing stage, before the software runs. It's like a warning system alerting us that the enclosure gate is open, before the dinosaur has a chance to escape!
Like the monitoring system in Jurassic Park, TypeScript detects potential problems even at the code-writing stage:
1function feedDinosaur(dinosaur: { species: string; dietType: string; }): void {
2 let food;
3
4 if (dinosaur.dietType === 'herbivore') {
5 food = 'fern leaves';
6 } else if (dinosaur.dietType === 'carnivore') {
7 food = 'meat';
8 }
9
10 console.log(`Feeding ${dinosaur.species} with food: ${food}`);
11}
12
13// TypeScript will warn that the typo 'herbivro' matches no condition
14const triceratops = { species: 'Triceratops', dietType: 'herbivro' };
15feedDinosaur(triceratops);
16// Without TypeScript this dinosaur would receive "undefined" as food!TypeScript acts like a detailed inventory of all dinosaurs in the park:
1// Without TypeScript - what are these parameters? What does this function return?
2function createEnclosure(id, name, capacity, fenceType) {
3 //
4}
5
6// With TypeScript - instant documentation!
7function createEnclosure(
8 id: number,
9 name: string,
10 capacity: number,
11 fenceType: 'standard' | 'electric' | 'reinforced'
12): { id: number; status: 'active' | 'under construction' | 'closed' } {
13 //
14 return { id, status: 'under construction' };
15}Thanks to TypeScript, your development environment (IDE) will know exactly what operations you can perform on a given object:

It's like a map of Jurassic Park with detailed directions showing where the individual enclosures are and what dinosaurs are kept in them.
When you make changes to the park management system, TypeScript warns you about all places that need updating:
1// Before refactoring
2interface Dinosaur {
3 id: number;
4 species: string;
5 dangerous: boolean;
6}
7
8// After refactoring - changing the field type
9interface Dinosaur {
10 id: number;
11 species: string;
12 threatLevel: number; // 1 to 10 instead of boolean
13}
14
15// TypeScript will point to all places where we used "dangerous"
16// and now we need to use "threatLevel"Even if you use external libraries written in plain JavaScript, you can use type definition files that provide code completion and type checking:
1// Installing types for the DNA analysis library
2// npm install --save-dev @types/dino-dna-analyzer
3
4import { analyzeDNA } from 'dino-dna-analyzer';
5
6// Now TypeScript knows the types of parameters and return values
7const result = analyzeDNA({
8 sequence: 'ACGTACGT',
9 baseSpecies: 'frog'
10});
11
12console.log(result.completeness); // TypeScript knows the available fieldsTypeScript lets you use the latest JavaScript features even if the target environment doesn't support them:
1// Using modern JavaScript syntax
2const dinosaurs = ['T-Rex', 'Velociraptor', 'Triceratops'];
3const [mostDangerous, ...rest] = dinosaurs;
4
5// Optional chaining and nullish coalescing
6const data = {
7 park: {
8 // sector may not exist
9 }
10};
11
12const sectorName = data.park?.sector?.name ?? "Unknown sector";One of the biggest advantages of TypeScript is the ability to gradually introduce it to existing JavaScript projects:
.jsto.ts// @ts-ignore or the any typeIt's like gradually upgrading the security systems in Jurassic Park, sector by sector, instead of shutting down the entire park for renovations.
Of course, like every technology, TypeScript has its limitations:
any type - you can "cheat" the type system by using any, which negates TypeScript's advantagesTypeScript works best in the following scenarios:
TypeScript is like an advanced security system for Jurassic Park:
By adding typing to JavaScript code, you gain:
In the next lessons we'll start exploring the TypeScript type system in detail and see how we can use it to build more reliable systems - ones that wouldn't let dinosaurs escape from the park!