What Is TypeScript? Explained Simply for Beginners

2026-11-28来源 dev.toEnglish
What Is TypeScript? Explained Simply for Beginners

Originally published on CodingCush.

TypeScript is JavaScript with syntax for types. That is the official one line definition. You write normal JavaScript, you add small labels that say what kind of value each variable holds, and a tool called the TypeScript compiler checks those labels before your code ever runs. The result is still plain JavaScript, so it runs anywhere JavaScript runs: in a browser, on Node.js, and in your apps. TypeScript does not replace JavaScript. It sits on top of JavaScript and catches mistakes early.

Learn TypeScript in 5 Small Steps

Five small steps will take you from zero to typed code that catches real mistakes. Each step adds one idea and one short example.

Step 1: Install TypeScript and Meet the Compiler

TypeScript is a tool you install through npm, the Node.js package manager, so you need Node.js first. If you have never met Node.js, read our guide to what Node.js is, explained simply first. Then run one command:

npm install -g typescript 

The -g flag installs it globally, so tsc works from any folder. Check it with tsc -v. Now create a file called hello.ts and type this:

const message: string = "Hello, TypeScript!"; console.log(message); 

The : string part is a type annotation, telling TypeScript that message will always hold text. Now compile the file into plain JavaScript:

tsc hello.ts 

TypeScript creates hello.js next to it. Open it and you will see the type has vanished:

const message = "Hello, TypeScript!"; console.log(message); 

Types exist only while you write and check code. Run the JavaScript file with node hello.js and you get:

Hello, TypeScript! 

Step 2: Add Your First Type Annotation

A type annotation is a small label after a name, written with a colon. The common types are string for text, number for numbers, and boolean for true or false. Here is a tiny function with annotations on its input and its output:

function greet(name: string): string { return "Hello, " + name + "!"; } console.log(greet("Priya")); 

The first : string says the name must be text. The second says the function returns text. Compile it with tsc, run it with node, and you get:

Hello, Priya! 

Now call greet(42) instead of greet("Priya"). TypeScript stops you before the code runs:

Argument of type 'number' is not assignable to parameter of type 'string'. 

In plain JavaScript, that mistake would run and print "Hello, 42!". In TypeScript, you find out in seconds, and that early warning is the main reason developers use types.

Screenshot of the TypeScript Handbook landing page on typescriptlang.org, showing the handbook navigation and the About this Handbook section

The TypeScript Handbook landing page, the official starting point for learning TypeScript.

Step 3: Let TypeScript Guess the Type for You

You do not have to label everything. TypeScript can often work out the type from the value you give it. This is called type inference. Here is the handbook example:

let helloWorld = "Hello World"; 

You never wrote : string, but TypeScript knows helloWorld is a string, because "Hello World" is text. Add labels where they help, and let TypeScript fill in the rest.

Inference also powers the red squiggle in your code editor. Look at this example from the official TypeScript homepage:

const user = { firstName: "Angela", lastName: "Davis", role: "Professor", }; console.log(user.name); 

There is no property called name here, only firstName, lastName, and role. TypeScript spots the typo at once:

Property 'name' does not exist on type '{ firstName: string; lastName: string; role: string; }'. 

In plain JavaScript, console.log(user.name) would print undefined, and you might spend an hour wondering why your page is blank. TypeScript tells you the problem at once.

Step 4: Describe an Object With an Interface

Real programs pass objects around, and an object with the wrong shape is a classic source of bugs. An interface describes the shape an object must have. This example comes from the official handbook:

interface User { name: string; id: number; } const user: User = { name: "Hayes", id: 0, }; console.log(user.name + " has id " + user.id); 

A User has a name that is text and an id that is a number. Run it and you get:

Hayes has id 0 

Now mistype a property name:

const user: User = { username: "Hayes", id: 0, }; 

TypeScript replies:

Object literal may only specify known properties, and 'username' does not exist in type 'User'. 

That message tells you exactly which word is wrong. Interfaces also work as documentation: a teammate can read one and see at a glance what a User looks like. The official TypeScript for JavaScript Programmers guide uses the same User example and shows interfaces with classes and functions too.

Step 5: Combine Types With a Union

Sometimes a value can be one of a few options only. A union type lists them with a pipe character between them. Here is an example from the official TypeScript homepage:

type Result = "pass" | "fail"; function verify(result: Result) { if (result === "pass") { console.log("Passed"); } else { console.log("Failed"); } } verify("pass"); verify("fail"); 

Run it and you get:

Passed Failed 

The clever part is what happens when you make a mistake. Try verify("maybe"). TypeScript refuses, because "maybe" is not in the union. A union turns a value into a short menu of allowed choices, a pattern used for loading states, button sizes, user roles, and form steps. The compiled JavaScript has every type removed. The official TypeScript Handbook explains unions and generics once you are ready for more.

TypeScript vs JavaScript: A Quick Comparison

The two languages are close cousins.

Feature JavaScript TypeScript
What it is The language of the web JavaScript with syntax for types
File ending .js .ts
Runs in a browser Yes, directly No, it compiles to JavaScript first
Catches type errors early No, they appear at run time Yes, in your editor before you run the code
Learning curve Gentle for beginners Gentle if you know JavaScript already
Best for Small scripts and first projects Larger apps, teams, and long lived code

You do not have to choose one and forget the other. The official docs say you can apply types step by step, and many developers add TypeScript file by file as the project grows.

Common Mistakes and How to Fix Them

Every beginner hits the same handful of snags.

tsc is not recognised as a command. TypeScript is not installed yet, or it was installed without the -g flag. Run npm install -g typescript again, then check with tsc -v. Node.js must be installed first, or npm will not exist.

You tried to run a .ts file in the browser or with node. Browsers and Node.js run JavaScript, not TypeScript. Compile first with tsc hello.ts, then run the .js file.

Property 'x' does not exist on type. Check your spelling first. Most of the time it is a typo, like name instead of firstName. The error message lists the properties that do exist.

You used any for everything. The any type tells TypeScript to stop checking, which turns off the whole point of the tool. It is fine while you are learning, but replace any with a real type soon.

Questions Beginners Keep Asking

Is TypeScript a different language from JavaScript?

No. TypeScript is JavaScript with syntax for types. Your existing JavaScript code is also TypeScript code, and TypeScript code compiles down to plain JavaScript.

Do I need to learn JavaScript first?

Yes. The official handbook recommends learning JavaScript first, because TypeScript builds directly on it.

Does TypeScript run in the browser?

No. Browsers only understand JavaScript. You write TypeScript, tsc turns it into JavaScript, and the browser runs that.

Is TypeScript free?

Yes. TypeScript is free and open source, and the official handbook and playground are free too.

Do I need TypeScript for small projects?

No. For a tiny script, plain JavaScript is fine. TypeScript pays off as a project grows, and you can add types later, one file at a time.

Will learning TypeScript help me get a front-end job?

Yes, it helps. Many front-end jobs list TypeScript alongside JavaScript, and popular tools such as React are commonly used with it. For the full picture, read our guide to what React is, explained simply and our roadmap on how to become a front-end developer.

Your Turn: Write a Small Typed Program

In short, TypeScript is JavaScript with syntax for types. It checks your code before it runs, catches typos and wrong values early, and compiles to plain JavaScript that runs anywhere. Learn it in five small steps: install it, annotate a value, let it infer types, describe objects with interfaces, and combine types with unions.

Now it is your turn. Create profile.ts, define an interface called Profile with a name, an age, and a favourite hobby, and write a function that prints a friendly sentence about your profile. Then change the age to a word like "twenty" and watch TypeScript catch it. That one red error message is the whole idea, and now you know how to read it.

原文:https://www.a8q9.cn/en/article/what-is-typescript-explained-simply-for-beginners-65bdd8.html

相关文章