What is TypeScript and where it runs

What TypeScript is

TypeScript is a language built on top of JavaScript that adds static types. You write familiar JavaScript code plus short notes about which values are numbers, strings, arrays, and objects. A compiler then checks those notes before the program ever runs.

function add(a: number, b: number): number { return a + b; } console.log(add(2, 3));

5

The add function above accepts two numbers and returns a number. When the call matches those notes, the compiler stays silent and the program prints 5.

Where it runs

TypeScript never runs directly. The tsc compiler erases the type notes and emits plain JavaScript, and that output runs in every place JavaScript runs: browsers, servers driven by Node, and desktop or mobile runtimes built on JavaScript engines.

function add(a, b) { return a + b; }

The compiled output keeps the logic and drops the annotations. Types exist only at compile time, so they add safety without slowing the running program.

Why types pay off

Types turn silent wrong behavior into loud early errors. The next line does not compile, and that is exactly the point. The compiler stops a mistake that plain JavaScript would accept and run with:

const age: number = "thirty";

main.ts(1,7): error TS2322: Type 'string' is not assignable to type 'number'.

Large projects gain the most. Type notes document what each function expects, editors suggest correct members while you type, and refactors become safer because the compiler lists every spot that still uses the old shape.

How it relates to JavaScript

Every valid JavaScript program is already a valid TypeScript program. You can rename a js file to ts, compile it at once, and add type notes step by step where they help the most.

Plain JavaScript becomes typed TypeScript by adding two short notes:

function greet(name) { return "Hello, " + name; } function greetTyped(name: string): string { return "Hello, " + name; }

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does a type annotation such as : number do for a running program?

    Answer

    It is checked by the compiler and removed from the output

    Types live in the source: tsc checks them and writes plain JavaScript without them.

  2. When does TypeScript report a type error?

    Answer

    Before the program runs, while it compiles

    The compiler checks the source and stops the build, so the program with the mistake never starts.

  3. What does this program print? const n: number = 2; console.log(n + 3);

    Answer

    5

    Both operands are numbers, so + adds them and the program prints 5.

  4. What does this program print? function double(value: number): number { return value * 2; } console.log(double(21));

    Answer

    42

    The call passes a number and the function returns that number doubled.

  5. What does the compiler say about this program? const age: number = "thirty"; console.log(age);

    Answer

    error TS2322: Type 'string' is not assignable to type 'number'.

    A string literal cannot be stored in a variable typed as number, and the compiler names both types.

  6. What does this program print? let city = "Paris"; console.log(city.length);

    Answer

    5

    The string is inferred from the starting value, and length counts its five characters.

  7. What does tsc write to disk when it compiles a .ts file?

    Answer

    Plain JavaScript without the type annotations

    The type notes only guide the compiler, so the emitted file is JavaScript that every engine runs.

  8. Where can the JavaScript that tsc emits run?

    Answer

    Anywhere JavaScript runs, browsers and Node included

    The output is ordinary JavaScript, so the host only needs a JavaScript engine.

  9. What does this program print? const price: number = 10; console.log("Total: " + price);

    Answer

    Total: 10

    + with a string on the left joins the two values into one string.

  10. What does this program print? console.log(typeof "5");

    Answer

    string

    The quotes make a string, so typeof reports string even though the text looks like a number.

Article author: Arthur Isaev