Lesson 2 of 60 – What is TypeScript?
3%

What is TypeScript?

TypeScript is a programming language developed by Microsoft. It is built on top of JavaScript and adds powerful features such as static typing, interfaces, type checking, and better development tools.

Note: TypeScript code is converted into JavaScript before it runs in a browser or in environments such as React Native.

1. Definition of TypeScript

TypeScript is a strongly typed programming language based on JavaScript. It allows developers to specify what type of data a variable, function, or object should contain.

let name: string = "Rahul";

Here, name is declared as a string.

2. TypeScript is Developed by Microsoft

TypeScript was developed and is maintained by Microsoft. It was designed to make JavaScript development easier for large and complex applications.

Today, TypeScript is commonly used in modern web and application development.

3. TypeScript is Based on JavaScript

TypeScript is built on JavaScript. This means JavaScript knowledge is very useful when learning TypeScript.

let age = 20;

The above JavaScript code is also valid TypeScript code.

4. TypeScript Adds Types

The most important feature of TypeScript is its type system. You can specify the type of data stored in a variable.

let age: number = 20;
let name: string = "Amit";
let active: boolean = true;

This helps developers detect many mistakes while writing code.

5. TypeScript Helps Find Errors

TypeScript can detect many type-related errors before the program runs.

let age: number = 20;

age = "Twenty";

The second line produces a type error because a string is being assigned to a variable declared as a number.

6. TypeScript Uses Type Checking

Type checking means checking whether values are being used with the correct data types.

let marks: number = 90;

The variable marks should contain a number.

7. TypeScript Code Uses .ts Extension

Normal TypeScript files use the .ts extension.

app.ts
student.ts
calculator.ts

The .ts extension tells us that the file contains TypeScript code.

8. TypeScript with TSX

When TypeScript is used with JSX, the file normally uses the .tsx extension.

App.tsx
Home.tsx
Profile.tsx

This is especially useful when working with React and React Native.

9. TypeScript Does Not Run Directly

Browsers and JavaScript environments normally execute JavaScript. Therefore, TypeScript code is compiled into JavaScript.

TypeScript
    ↓
Compiler
    ↓
JavaScript
    ↓
Execution

10. TypeScript Compiler

The TypeScript compiler converts TypeScript code into JavaScript code. The compiler is commonly called tsc.

tsc app.ts

This command can compile app.ts into JavaScript.

11. TypeScript is a Superset of JavaScript

TypeScript is often described as a superset of JavaScript. This means valid JavaScript code can generally be used inside a TypeScript file.

let message = "Hello";

console.log(message);

This is valid TypeScript because it is also valid JavaScript.

12. Static Typing

TypeScript supports static typing. You can explicitly define the type of a variable.

let price: number = 500;

The variable price is expected to contain a number.

13. TypeScript and Variables

Types can be assigned to variables using a colon :.

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

This makes the expected data type clear.

14. TypeScript and Functions

TypeScript can also specify parameter and return types in functions.

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

Here both parameters and the return value are numbers.

15. TypeScript and Objects

TypeScript can describe the structure of an object.

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

This helps ensure that the object contains the expected properties.

16. TypeScript and Arrays

TypeScript can specify what type of values an array should contain.

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

This array is expected to contain numbers.

17. TypeScript and Interfaces

Interfaces allow developers to define the structure of objects.

interface Student {
    name: string;
    age: number;
}

The interface can then be used to describe student objects.

18. TypeScript and Classes

TypeScript supports classes and object-oriented programming.

class Student {
    name: string;

    constructor(name: string) {
        this.name = name;
    }
}

Classes can be useful for creating structured applications.

19. TypeScript Improves Code Readability

Type annotations make the expected data type clear.

function calculateTotal(price: number): number {
    return price + 100;
}

A developer can quickly understand what the function accepts and returns.

20. TypeScript Helps Large Projects

As an application becomes larger, managing many variables, functions, objects, and components can become difficult.

TypeScript helps developers organize and check the structure of their code.

21. TypeScript with React

TypeScript can be used to develop React applications.

function Welcome() {
    return <h1>Welcome</h1>;
}

With TypeScript, React components, props, state, and events can also be given appropriate types.

22. TypeScript with React Native

TypeScript can also be used with React Native to build mobile applications.

const App = () => {
    return (
        <View>
            <Text>Hello Mobile App</Text>
        </View>
    );
};

TypeScript helps provide type safety while developing React Native apps.

23. TypeScript and Developer Tools

TypeScript works well with modern code editors. Editors can provide features such as:

  • Code completion
  • Error highlighting
  • Type information
  • Suggestions
  • Refactoring support

24. TypeScript and Autocomplete

Because TypeScript understands the types of variables and objects, development tools can provide better autocomplete suggestions.

let student = {
    name: "Amit",
    age: 20
};

When working with student, the editor can understand its available properties.

25. TypeScript Reduces Common Mistakes

TypeScript can detect many common programming mistakes during development.

let age: number = 20;

// Error
age = "Hello";

Finding errors during development can make debugging easier.

26. TypeScript Code Can Use JavaScript Features

TypeScript supports modern JavaScript features such as:

  • let and const
  • Arrow functions
  • Classes
  • Promises
  • Destructuring
  • Modules

27. TypeScript is Useful for React Native Students

If you are learning React Native, TypeScript can help you write safer components, props, state, navigation parameters, API data, and forms.

For this reason, TypeScript is an important technology for modern React Native development.

28. Simple TypeScript Example

let name: string = "Rahul";
let age: number = 22;

function showStudent(
    studentName: string,
    studentAge: number
): void {
    console.log(studentName);
    console.log(studentAge);
}

showStudent(name, age);

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

29. TypeScript Development Flow

A simple TypeScript development flow is:

  1. Write TypeScript code.
  2. Check the code for type errors.
  3. Compile TypeScript into JavaScript.
  4. Run the resulting JavaScript.
TypeScript
   ↓
Type Checking
   ↓
Compilation
   ↓
JavaScript
   ↓
Application

30. Why Learn TypeScript?

TypeScript is useful because it provides a type system on top of JavaScript and improves the development experience for larger applications.

For a React Native developer, learning TypeScript will help you work with typed components, props, state, navigation, API responses, and reusable application code.

Remember: TypeScript extends JavaScript. TypeScript code is checked and then compiled into JavaScript.

📌 Key Points

  • TypeScript is developed by Microsoft.
  • TypeScript is based on JavaScript.
  • TypeScript adds a powerful type system.
  • TypeScript helps detect many errors during development.
  • TypeScript files normally use the .ts extension.
  • TSX files use the .tsx extension.
  • TypeScript is compiled into JavaScript.
  • TypeScript can be used with React.
  • TypeScript can be used with React Native.
  • TypeScript is useful for large applications.

🧠 Quick Quiz

Question: What is TypeScript?