TypeScript | The School of Code

Settings

Appearance

Choose a typography theme that suits your style

TypeScript

Add type safety to your JavaScript code.

1

Hello TypeScript

TypeScript is JavaScript with types. Types help you catch mistakes before your code runs, making development faster and more reliable.

Adding a type is simple - just use a colon after the variable name: let name: string = "Alice".

Example

Run this example to see how it works and then try the exercise.

example.ts
Loading...
Output
Click "Run" to see the example in action...
Exercise

Your task: Create a greeting message and print it

main.ts
Loading...
Output
Click "Run" to execute your code...
Quiz

What does TypeScript add to JavaScript?

SUMMARY

You just wrote your first TypeScript code! Type annotations help catch errors early. Next, you’ll learn about the basic types available in TypeScript.


2

Basic Types

TypeScript has three basic types that you’ll use constantly:

  • string for text
  • number for all numbers (no separate int/float)
  • boolean for true/false
Example

Run this example to see how it works and then try the exercise.

example.ts
Loading...
Output
Click "Run" to see the example in action...
Exercise

Your task: Create three variables with type annotations:

main.ts
Loading...
Output
Click "Run" to execute your code...
Quiz

What type would you use for the value 3.14?

SUMMARY

You learned the three fundamental types: string, number, and boolean. These cover most everyday programming needs. Next, you’ll see how TypeScript can figure out types automatically!


3

Type Inference

TypeScript has type inference - it can figure out types automatically when you assign a value. This means less typing for you while still getting type safety!

Use explicit types when clarity helps. Use inference when the type is obvious.

Example

Run this example to see how it works and then try the exercise.

example.ts
Loading...
Output
Click "Run" to see the example in action...
Exercise

Your task: TypeScript can infer types automatically!

main.ts
Loading...
Output
Click "Run" to execute your code...
Quiz

What type does TypeScript infer for: let x = 100?

SUMMARY

TypeScript can automatically figure out types from your values - this is called type inference. You don’t always need to write types explicitly! Next, you’ll learn how to type arrays.

Unlock 10 More Lessons

You've completed the free preview! Upgrade to Pro to access all 12 lessons in this course, plus advanced projects and certificates.

Locked lessons:
  • Arrays
  • Objects
  • Interfaces
  • Functions
  • Union Types
  • Type Aliases
  • Literal Types
  • Generics
  • Project: Type-Safe Task Manager

Reset Progress?

Are you sure you want to reset all progress for this course? This action cannot be undone.