Skip to content
Laviyx Learn

Try

TypeScript course menu

Your First TypeScript Program: Annotations, Errors and the Compiled JavaScript

Write a first TypeScript program step by step: add type annotations to variables and functions, read the compiler's error messages, see what the compiled JavaScript looks like, and understand that types are erased at runtime.

Beginner5 min readUpdated
On this page

Let us write a small program, deliberately break it, and see how TypeScript helps.

The program#

order.ts (TypeScript)
const customer: string = "Asha";
const items: number = 3;
const pricePerItem: number = 149.5;

function total(count: number, price: number): number {
  return count * price;
}

console.log(`${customer} ordered ${items} items`);
console.log(`Total: ₹${total(items, pricePerItem).toFixed(2)}`);
Output
Asha ordered 3 items
Total: ₹448.50

The new parts are the type annotations, written as a colon and a type after a name:

CodeMeans
customer: stringcustomer holds text
items: numberitems holds a number
count: number, price: numberThe function's parameters must be numbers
): number after the parametersThe function returns a number

Reading compiler errors#

Change the value of items to a string:

TypeScript
const items: number = "3";
Compiler error
error TS2322: Type 'string' is not assignable to type 'number'.

Misspell a method name:

TypeScript
const customer: string = "Asha";
console.log(customer.toUppercase());
Compiler error
error TS2551: Property 'toUppercase' does not exist on type 'string'. Did you mean 'toUpperCase'?

Call the function with the wrong number of arguments:

TypeScript
function total(count: number, price: number): number {
  return count * price;
}
total(3);
Compiler error
error TS2554: Expected 2 arguments, but got 1.

Each error has a code (TS2322), a message, and, in the full output, the file name, line and column. In VS Code, the same messages appear when you hover over the red underline. Searching for the error code online usually finds a good explanation.

What the compiler produces#

For the program above, tsc outputs JavaScript like this:

order.js (JavaScript)
const customer = "Asha";
const items = 3;
const pricePerItem = 149.5;
function total(count, price) {
    return count * price;
}
console.log(`${customer} ordered ${items} items`);
console.log(`Total: ₹${total(items, pricePerItem).toFixed(2)}`);

The annotations are simply removed. This is the most important idea in TypeScript: types exist only at compile time. At runtime, it is plain JavaScript, so TypeScript cannot check data that arrives while the program runs, such as user input or API responses. You will learn how to handle that safely in later lessons (any, unknown and never, Async Code and API Data).

You do not need to annotate everything#

TypeScript works out many types by itself. These two lines are equivalent:

TypeScript
const city: string = "Pune";
const town = "Pune";          // TypeScript infers that this is a string
console.log(city === town);
Output
true

Annotating function parameters is required, while most variables can rely on inference. The Type Inference lesson explains when to annotate.

Practice#

Practice exercise

Write a typed function bmi(weightKg: number, heightM: number): number and print the BMI for 68 kg and 1.72 m rounded to one decimal place. Then try calling it with a string to see the error.

Show solution
TypeScript
function bmi(weightKg: number, heightM: number): number {
  return weightKg / (heightM * heightM);
}

console.log(bmi(68, 1.72).toFixed(1));
Output
23.0
TypeScript
function bmi(weightKg: number, heightM: number): number {
  return weightKg / (heightM * heightM);
}
bmi("68", 1.72);
Compiler error
error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.

Key takeaway#

Finished this lesson?