Lesson 1 of 60 – Introduction to TypeScript
2%

Introduction to TypeScript

TypeScript is a programming language that is widely used for building modern web applications and applications with frameworks such as React and React Native.

TypeScript adds a powerful type system to JavaScript. It helps developers write more structured, readable, and maintainable code.

Note: TypeScript is especially useful in React Native because it helps us detect many coding mistakes while developing an application.

1. What is TypeScript?

TypeScript is a programming language developed by Microsoft. It is built on top of JavaScript and adds additional features such as static typing.

TypeScript code is eventually converted into JavaScript so that it can run in JavaScript environments.

let name: string = "Rahul";

Here, string specifies that the variable should contain text.

2. TypeScript and JavaScript

TypeScript is based on JavaScript. This means JavaScript developers can gradually learn TypeScript without learning an entirely unrelated programming language.

JavaScript:

let age = 20;

TypeScript:

let age: number = 20;

TypeScript provides additional information about the expected type of a value.

3. Why Was TypeScript Created?

Large JavaScript applications can become difficult to maintain. As projects become larger, finding mistakes and understanding the structure of data can become more difficult.

TypeScript helps developers by providing features such as:

  • Static type checking
  • Interfaces
  • Type aliases
  • Generics
  • Better editor support

4. TypeScript is a Superset of JavaScript

TypeScript is often described as a superset of JavaScript.

This means that valid JavaScript code can generally be used in a TypeScript project.

let message = "Hello";

console.log(message);

This is valid JavaScript and is also valid TypeScript.

5. TypeScript Adds Static Typing

One of the most important features of TypeScript is static typing.

let age: number = 25;

let name: string = "Amit";

let isStudent: boolean = true;

The types tell TypeScript what kind of values these variables are expected to contain.

6. What is a Type?

A type describes what kind of value a variable can contain.

Common TypeScript types include:

  • string
  • number
  • boolean
  • array
  • object
  • tuple
  • any
  • unknown
let city: string = "Patna";
let age: number = 25;
let active: boolean = true;

7. TypeScript Example

Consider the following TypeScript code:

let studentName: string = "Rahul";
let age: number = 21;
let passed: boolean = true;

console.log(studentName);
console.log(age);
console.log(passed);

Each variable has a declared type.

8. TypeScript Helps Find Errors

TypeScript can detect many type-related mistakes before the application runs.

let age: number = 25;

age = "twenty";

The variable is declared as a number, but a string is assigned to it. TypeScript can report this as a type error.

9. TypeScript and Code Editors

TypeScript works especially well with modern code editors. Editors can provide useful information while writing TypeScript code.

For example, the editor can provide:

  • Code completion
  • Error highlighting
  • Type information
  • Suggestions
  • Automatic navigation

10. TypeScript in React

TypeScript is commonly used with React applications.

It can help developers define the expected structure of component properties, state, functions, and other data.

type User = {
    name: string;
    age: number;
};

This describes the structure of a User object.

11. TypeScript in React Native

TypeScript is also very useful when developing React Native mobile applications.

It can be used to describe:

  • Component props
  • State values
  • Navigation parameters
  • API responses
  • User data
  • Form data

12. Example for React Native

A user object in a React Native application can be described using TypeScript.

type User = {
    id: number;
    name: string;
    email: string;
};

const user: User = {
    id: 1,
    name: "Rahul",
    email: "rahul@example.com"
};

This makes the expected structure of the user data clear.

13. TypeScript File Extension

TypeScript files normally use the .ts extension.

app.ts

When TypeScript is used with JSX, the .tsx extension is commonly used.

App.tsx

14. .ts and .tsx Files

The two important TypeScript file extensions are:

Extension Purpose
.ts TypeScript code without JSX
.tsx TypeScript code that contains JSX

React and React Native components commonly use .tsx files when JSX is included.

15. TypeScript Compilation

Browsers and JavaScript environments execute JavaScript. TypeScript is therefore processed into JavaScript.

The TypeScript compiler performs this conversion.

TypeScript Code
       ↓
TypeScript Compiler
       ↓
JavaScript Code

16. The TypeScript Compiler

The TypeScript compiler is commonly called tsc.

It checks TypeScript code and can generate JavaScript output.

tsc app.ts

This command tells the TypeScript compiler to process the file named app.ts.

17. TypeScript is Not a Separate Runtime

TypeScript provides development-time features such as type checking. The final application normally runs JavaScript.

This is especially important to understand when learning React Native with TypeScript.

TypeScript
    ↓
JavaScript
    ↓
Application Runtime

18. TypeScript and Large Projects

TypeScript becomes particularly useful as an application grows.

In a large project, many developers may work with the same codebase. Types help communicate how data and functions are expected to work.

This can make the code easier to understand and maintain.

19. TypeScript Improves Code Readability

Type annotations can make the purpose of variables clear.

let username: string = "Amit";
let age: number = 22;
let loggedIn: boolean = true;

From the code itself, we can understand what type of value each variable is expected to contain.

20. TypeScript and Maintainability

Maintainability means making code easier to understand, modify, and extend over time.

TypeScript can help by providing:

  • Clear data structures
  • Type checking
  • Better editor support
  • Safer refactoring
  • Clearer function definitions

21. TypeScript and Functions

TypeScript can specify the types of function parameters and return values.

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

console.log(add(10, 20));

Here both parameters and the returned value are numbers.

22. TypeScript and Objects

TypeScript can describe the expected structure of objects.

let student: {
    name: string;
    age: number;
} = {
    name: "Priya",
    age: 20
};

The object must follow the specified structure.

23. TypeScript and Arrays

TypeScript can specify the type of values stored in an array.

let marks: number[] = [80, 75, 90];

let names: string[] = [
    "Rahul",
    "Amit",
    "Priya"
];

The first array contains numbers and the second contains strings.

24. TypeScript and Modern Development

TypeScript is commonly used in modern application development.

It can be used with technologies such as:

  • React
  • React Native
  • Node.js
  • Angular
  • Next.js

In this tutorial, our main focus will be TypeScript for React Native development.

25. TypeScript Learning Path

To learn TypeScript effectively, we will progress from basic concepts to advanced concepts.

  1. Types and variables
  2. Functions
  3. Objects
  4. Interfaces
  5. Union types
  6. Generics
  7. Classes
  8. TypeScript with React
  9. TypeScript with React Native

26. What You Will Learn in This Course

During this tutorial series, you will learn how to:

  • Create TypeScript variables.
  • Use different data types.
  • Create typed functions.
  • Work with objects and arrays.
  • Create interfaces.
  • Use advanced types.
  • Use generics.
  • Use TypeScript with React.
  • Use TypeScript with React Native.
  • Build practical TypeScript-based applications.

27. First Simple TypeScript Example

Here is a simple TypeScript program:

let studentName: string = "Rahul";
let age: number = 20;

function displayStudent(): void {
    console.log(studentName);
    console.log(age);
}

displayStudent();

This example uses variables, type annotations, a function, and a return type.

28. TypeScript for React Native Students

If you already know JavaScript and React, TypeScript can be learned progressively.

You do not need to stop learning React Native. Instead, you will learn how TypeScript can be used to make React Native code more structured and type-safe.

Later lessons will connect TypeScript concepts directly with React Native components and application development.

29. Important Terms

  • TypeScript: A programming language based on JavaScript.
  • Type: Describes the kind of value a variable can contain.
  • Type Annotation: Explicitly specifies a type.
  • Compiler: Processes TypeScript code into JavaScript.
  • ts: TypeScript file extension.
  • tsx: TypeScript file extension commonly used with JSX.

30. Summary of TypeScript

TypeScript extends JavaScript with a powerful type system and additional development features.

It can help developers identify type-related mistakes, understand data structures, and maintain large applications.

Since you are learning TypeScript for React Native, the upcoming lessons will gradually move from basic TypeScript concepts toward practical React and React Native development.

JavaScript
    +
Type System
    +
Modern Development Features
    =
TypeScript

📌 Key Points

  • TypeScript is developed by Microsoft.
  • TypeScript is based on JavaScript.
  • TypeScript adds static typing to JavaScript.
  • TypeScript code is processed into JavaScript.
  • TypeScript files normally use the .ts extension.
  • TSX files are commonly used when TypeScript contains JSX.
  • TypeScript can help identify type-related errors during development.
  • TypeScript is widely used with React and React Native.

🧠 Quick Quiz

Question: What is TypeScript?