Mamun Labs Learn
JourneyGlossary
esc
  • Try “map”, “flexbox” or “state”.
Log in
Start learning
Phase 4 · 2 / 5Reading type annotations
Phase 4TypeScript
5 topics · 2 wks
  1. 1Why types catch bugs
  2. 2Reading type annotations
  3. 3Types for objects
  4. 4Unions and optional values
  5. 5Reading type errors
JourneyPhase 4Reading type annotations
01 / 09Overview

Reading type annotations

You'll learn to read type annotations: : string, : number[], and the types on a function's inputs and output.

Why it mattersEvery function in a React Native project tells you its inputs and output through types. Reading them saves you from guessing.
about 60 min·needs: Why types catch bugs
02 / 09

Watch & read

Start with the required ones. The others are there when you need them.

Docs
Everyday typesTypeScript Handbook (official) · 15 min · English
MMamun's noteRead from the top through "Functions" (parameter and return type annotations), then stop at "Object Types". That comes in the next topic. Skip the any part for now.
Video
TypeScript tutorial #3: Type basicsYouTube · Net Ninja · 8 min · English
MMamun's noteA short video on string, number and boolean, and the red lines you get when you mix them up. Watch all 7:35. He runs the code from the terminal; you don't need to copy his setup.
ArticleOptional
Essential types and annotationsTotal TypeScript Essentials · Matt Pocock (free online book) · 20 min · English
MMamun's noteFree chapter from Matt Pocock's book. Read "Basic Annotations" to "Type Inference", then "Arrays". The error boxes show real TypeScript messages. Stop before "Object Literal Types".
VideoOptional
Lesson 3: Type basics, TypeScript Bangla tutorial seriesYouTube · Learn with Sumit · 13 min · Bangla
MMamun's noteIn Bangla. Sumit goes through the common types one by one. Good if you want the same ideas explained slowly in Bangla.
03 / 09

Explanation

You open a file AI wrote and see function total(list: number[]): number. It looks like JavaScript with extra bits. Those extra bits are types, and they tell you exactly how to use the function.

Think of a form at the bank. Each box has a small label under it: "name, in letters", "amount, in numbers". The label doesn't fill the box. It says what is allowed in it. A type annotation is that label.

Read the colon as "is a". city: string means "city is a string".

let city: string = "Dhaka";
const isPaid: boolean = false;
const prices: number[] = [120, 60, 250];
const names: Array<string> = ["Rafi", "Nusrat"];

The basic types are string (text), number (any number, with or without decimals) and boolean (true or false). Add [] for a list: number[] is "an array of numbers". Array<string> is the same as string[], written another way. You'll see the < > form in React too: useState<number>(0) means "this state holds a number". The type inside < > is what goes inside.

Functions get labels on each parameter, and one more after the ) for what comes back:

function total(list: number[]): number {
let sum = 0;
for (const p of list) sum += p;
return sum;
}

list: number[] says what goes in. The : number after ) is the return type: what comes out. An arrow function has the same parts: (n: number): number => n / 2.

Often there is no label at all. In let marks = 85;, TypeScript sees 85 and works out that marks is a number by itself. This is called type inference. In VS Code, hover over any name to see its type.

People often mix up the two colons. Inside an object, { name: "Rafi" }, the colon gives a value. After a variable or parameter, name: string, the colon gives a type. If the right side is a real value ("Rafi", 85), it is an object. If it is a type word (string, number), it is a label.

When you read a typed function, read it like a sentence: "total takes a list of numbers and gives back a number."

Words to recognise
type annotationstringnumberbooleannumber[]Array<string>return typetype inference
04 / 09

Code tour

Click a line number with a ring to read the note for that line.

app.ts6 notes
let city: string = "Dhaka";
Mentor note · line 1
Read it as "city is a string". The label sits between the name and the =.
let marks = 85;
3const isPaid: boolean = false;
const prices: number[] = [120, 60, 250];
const names: Array<string> = ["Rafi", "Nusrat"];
6
function total(list: number[]): number {
8 let sum = 0;
9 for (const p of list) sum += p;
10 return sum;
11}
12
const half = (n: number): number => n / 2;
05 / 09

Practice

Short drills. Take your time and read the code before you answer.

DrillPredict1 / 2
1const marks = [80, 95, 70];
What is the type of marks?
DrillPredict2 / 2
1function greet(name: string, year: number): string {
2 return `Hi ${name}, year ${year}`;
3}
4greet("Rafi", 2);
5greet(2, "Rafi");
6greet("Nusrat", 3);
Which line gets a red underline?
06 / 09

Quick check

3 questions. Each one explains itself after you answer.

  1. 1 / 3In function total(list: number[]): number, what does the last number mean?
  2. 2 / 3Which type is the same as string[]?
  3. 3 / 3let city = "Dhaka"; has no type label. What type does TypeScript give city?
07 / 09

Explain it back

Only you can see this. Log in to keep it.0 words
08 / 09

How confident are you with Reading type annotations?

09 / 09

Done

You can now
Read a typed function like function total(prices: number[]): number and say what goes in and what comes out.
Next: Types for objects Back to Phase 4
On this page
  1. 01Reading type annotations
  2. 02Watch & read
  3. 03Explanation
  4. 04Code tour
  5. 05Practice
  6. 06Quick check
  7. 07Explain it back
  8. 08Confidence
  9. 09Done
Up next · 02 / 09Watch & read
Mamun Labs LearnFound a mistake on this page? Tell Mamun.
Mamun Labs Learn
JourneyGlossary
esc
  • Try “map”, “flexbox” or “state”.
Log in
Start learning
Log in to save your progress. Everything here works without an account.Log in to save progressLog in
Phase 4TypeScript
5 topics · 2 wks
  1. 1Why types catch bugs
  2. 2Reading type annotations
  3. 3Types for objects
  4. 4Unions and optional values
  5. 5Reading type errors
JourneyPhase 4Reading type annotations
01 / 09Overview

Reading type annotations

You'll learn to read type annotations: : string, : number[], and the types on a function's inputs and output.

Why it mattersEvery function in a React Native project tells you its inputs and output through types. Reading them saves you from guessing.
about 60 min·needs: Why types catch bugs
02 / 09

Watch & read

Start with the required ones. The others are there when you need them.

Docs
Everyday typesTypeScript Handbook (official) · 15 min · English
MMamun's noteRead from the top through "Functions" (parameter and return type annotations), then stop at "Object Types". That comes in the next topic. Skip the any part for now.
Video
TypeScript tutorial #3: Type basicsYouTube · Net Ninja · 8 min · English
MMamun's noteA short video on string, number and boolean, and the red lines you get when you mix them up. Watch all 7:35. He runs the code from the terminal; you don't need to copy his setup.
ArticleOptional
Essential types and annotationsTotal TypeScript Essentials · Matt Pocock (free online book) · 20 min · English
MMamun's noteFree chapter from Matt Pocock's book. Read "Basic Annotations" to "Type Inference", then "Arrays". The error boxes show real TypeScript messages. Stop before "Object Literal Types".
VideoOptional
Lesson 3: Type basics, TypeScript Bangla tutorial seriesYouTube · Learn with Sumit · 13 min · Bangla
MMamun's noteIn Bangla. Sumit goes through the common types one by one. Good if you want the same ideas explained slowly in Bangla.
03 / 09

Explanation

You open a file AI wrote and see function total(list: number[]): number. It looks like JavaScript with extra bits. Those extra bits are types, and they tell you exactly how to use the function.

Think of a form at the bank. Each box has a small label under it: "name, in letters", "amount, in numbers". The label doesn't fill the box. It says what is allowed in it. A type annotation is that label.

Read the colon as "is a". city: string means "city is a string".

let city: string = "Dhaka";
const isPaid: boolean = false;
const prices: number[] = [120, 60, 250];
const names: Array<string> = ["Rafi", "Nusrat"];

The basic types are string (text), number (any number, with or without decimals) and boolean (true or false). Add [] for a list: number[] is "an array of numbers". Array<string> is the same as string[], written another way. You'll see the < > form in React too: useState<number>(0) means "this state holds a number". The type inside < > is what goes inside.

Functions get labels on each parameter, and one more after the ) for what comes back:

function total(list: number[]): number {
let sum = 0;
for (const p of list) sum += p;
return sum;
}

list: number[] says what goes in. The : number after ) is the return type: what comes out. An arrow function has the same parts: (n: number): number => n / 2.

Often there is no label at all. In let marks = 85;, TypeScript sees 85 and works out that marks is a number by itself. This is called type inference. In VS Code, hover over any name to see its type.

People often mix up the two colons. Inside an object, { name: "Rafi" }, the colon gives a value. After a variable or parameter, name: string, the colon gives a type. If the right side is a real value ("Rafi", 85), it is an object. If it is a type word (string, number), it is a label.

When you read a typed function, read it like a sentence: "total takes a list of numbers and gives back a number."

Words to recognise
type annotationstringnumberbooleannumber[]Array<string>return typetype inference
04 / 09

Code tour

Click a line number with a ring to read the note for that line.

app.ts6 notes
let city: string = "Dhaka";
Mentor note · line 1
Read it as "city is a string". The label sits between the name and the =.
let marks = 85;
3const isPaid: boolean = false;
const prices: number[] = [120, 60, 250];
const names: Array<string> = ["Rafi", "Nusrat"];
6
function total(list: number[]): number {
8 let sum = 0;
9 for (const p of list) sum += p;
10 return sum;
11}
12
const half = (n: number): number => n / 2;
05 / 09

Practice

Short drills. Take your time and read the code before you answer.

DrillPredict1 / 2
1const marks = [80, 95, 70];
What is the type of marks?
DrillPredict2 / 2
1function greet(name: string, year: number): string {
2 return `Hi ${name}, year ${year}`;
3}
4greet("Rafi", 2);
5greet(2, "Rafi");
6greet("Nusrat", 3);
Which line gets a red underline?
06 / 09

Quick check

3 questions. Each one explains itself after you answer.

  1. 1 / 3In function total(list: number[]): number, what does the last number mean?
  2. 2 / 3Which type is the same as string[]?
  3. 3 / 3let city = "Dhaka"; has no type label. What type does TypeScript give city?
07 / 09

Explain it back

Only you can see this. Log in to keep it.0 words
08 / 09

How confident are you with Reading type annotations?

09 / 09

Done

You can now
Read a typed function like function total(prices: number[]): number and say what goes in and what comes out.
Next: Types for objects Back to Phase 4
On this page
  1. 01Reading type annotations
  2. 02Watch & read
  3. 03Explanation
  4. 04Code tour
  5. 05Practice
  6. 06Quick check
  7. 07Explain it back
  8. 08Confidence
  9. 09Done
Up next · 02 / 09Watch & read

You open a file AI wrote and see function total(list: number[]): number. It looks like JavaScript with extra bits. Those extra bits are types, and they tell you exactly how to use the function.

Think of a form at the bank. Each box has a small label under it: "name, in letters", "amount, in numbers". The label doesn't fill the box. It says what is allowed in it. A type annotation is that label.

Read the colon as "is a". city: string means "city is a string".

let city: string = "Dhaka";
const isPaid: boolean = false;
const prices: number[] = [120, 60, 250];
const names: Array<string> = ["Rafi", "Nusrat"];

The basic types are string (text), number (any number, with or without decimals) and boolean (true or false). Add [] for a list: number[] is "an array of numbers". Array<string> is the same as string[], written another way. You'll see the < > form in React too: useState<number>(0) means "this state holds a number". The type inside < > is what goes inside.

Functions get labels on each parameter, and one more after the ) for what comes back:

function total(list: number[]): number {
let sum = 0;
for (const p of list) sum += p;
return sum;
}

list: number[] says what goes in. The : number after ) is the return type: what comes out. An arrow function has the same parts: (n: number): number => n / 2.

Often there is no label at all. In let marks = 85;, TypeScript sees 85 and works out that marks is a number by itself. This is called type inference. In VS Code, hover over any name to see its type.

People often mix up the two colons. Inside an object, { name: "Rafi" }, the colon gives a value. After a variable or parameter, name: string, the colon gives a type. If the right side is a real value ("Rafi", 85), it is an object. If it is a type word (string, number), it is a label.

When you read a typed function, read it like a sentence: "total takes a list of numbers and gives back a number."

Words to recognise
type annotationstringnumberbooleannumber[]Array<string>return typetype inference

You open a file AI wrote and see function total(list: number[]): number. It looks like JavaScript with extra bits. Those extra bits are types, and they tell you exactly how to use the function.

Think of a form at the bank. Each box has a small label under it: "name, in letters", "amount, in numbers". The label doesn't fill the box. It says what is allowed in it. A type annotation is that label.

Read the colon as "is a". city: string means "city is a string".

let city: string = "Dhaka";
const isPaid: boolean = false;
const prices: number[] = [120, 60, 250];
const names: Array<string> = ["Rafi", "Nusrat"];

The basic types are string (text), number (any number, with or without decimals) and boolean (true or false). Add [] for a list: number[] is "an array of numbers". Array<string> is the same as string[], written another way. You'll see the < > form in React too: useState<number>(0) means "this state holds a number". The type inside < > is what goes inside.

Functions get labels on each parameter, and one more after the ) for what comes back:

function total(list: number[]): number {
let sum = 0;
for (const p of list) sum += p;
return sum;
}

list: number[] says what goes in. The : number after ) is the return type: what comes out. An arrow function has the same parts: (n: number): number => n / 2.

Often there is no label at all. In let marks = 85;, TypeScript sees 85 and works out that marks is a number by itself. This is called type inference. In VS Code, hover over any name to see its type.

People often mix up the two colons. Inside an object, { name: "Rafi" }, the colon gives a value. After a variable or parameter, name: string, the colon gives a type. If the right side is a real value ("Rafi", 85), it is an object. If it is a type word (string, number), it is a label.

When you read a typed function, read it like a sentence: "total takes a list of numbers and gives back a number."

Words to recognise
type annotationstringnumberbooleannumber[]Array<string>return typetype inference