> For the complete documentation index, see [llms.txt](https://sandeepamaranath.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sandeepamaranath.gitbook.io/notes/languages-and-frameworks/typescript.md).

# Typescript 🚀

By Max - Udemy

## Overview of Typescript?

Tool used only for development (to catch programmer bugs or human mistakes while writing code), and it doesn't do anything else apart from making developer life easy. Once it's compiled into JS, then the underlying JS code will not have any of this TS features the developer wrote. Browsers or JS engines can't understand typescript. So during run time, it's still the JS that gets executed, but we can be sure that the JS (we get after compiled by TS) will not have bugs or will have very less bugs (as TS might have caught during development and would have made the developer to write clean code).

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FbGdKzppd4GdiaK4bXlRv%2Fimage.png?alt=media&amp;token=9a55fc3b-878d-4431-b297-d5b1900fbe0e" alt=""><figcaption><p>what is ts</p></figcaption></figure>

* JS superset
* Language that's built on JS
* Adds new features + advantages to JS
* BROWSERS CAN'T EXECUTE TS
* It's a better version of JS
* It's not only a programming language but also a tool, a compiler to run over typescript code and compiles it to JavaScript
* Result of writing TS Code is JavaScript, but we didn't write that JS, we wrote TS

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FoIDP4N3UITR3NOrogWkY%2Fimage.png?alt=media&amp;token=d57dfc58-a2b3-4a14-97e5-44a1e05ae060" alt=""><figcaption><p>TS gets compiled to JS</p></figcaption></figure>

#### So one question to ask is, if typescript gives back JS in the end, then why we need typescript. What extra things TS can do that JS can't?

* The TS compiler compiles these new features (TS code) to JS "workarounds"
* So in the end it gives you nicer syntax and easier way of doing somethings and then it will compile this nicer easier way to more complex JS snippets which we would have to write otherwise
* Of course it can't add what's not possible in JS
* The other main feature of TS is, it add types which will make it possible to catch errors before it converts into JS. That way, when it compiles, it means that we have checked all the types and all JS code is good without any bugs or confusions that could otherwise occur when running that JS in browser

Let's say we write plain JS code like this. We can get into logical mistake like below. This is not an error but the logical mistake that a developer could do

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FwfCQdbSknJjXuQqVwxzq%2Fimage.png?alt=media&amp;token=0b837a59-3cfe-408a-851d-83c72693bc57" alt=""><figcaption></figcaption></figure>

This is how TS solves it. It lets developer know first before converting the code into JS

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FAXHmPSeSVqLrcpAs6TCV%2Fimage.png?alt=media&amp;token=163686e8-e874-4c8c-901b-7138651c7a0f" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2Fd5LMJBL7CoWzZk6odvBJ%2Fimage.png?alt=media&amp;token=cf10d995-4711-4b5f-9457-23eb1fe97eff" alt=""><figcaption></figcaption></figure>

of course we could make some additional checks to prevent this in JS like this, but ts can prevent us from falling into this in the first place

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FZ3MdNZvWy0ufPtbjHZsO%2Fimage.png?alt=media&amp;token=8876361f-368d-4777-8e2e-e25cd4233817" alt=""><figcaption></figcaption></figure>

TS can check if we are passing numbers and not strings. TS could throw error if we pass strings in place of numbers.

#### How to install TS

{% embed url="<https://www.typescriptlang.org/>" %}

```javascript
sudo npm install -g typescript
```

#### How to compile TS?

Now we can use `tsc` command to compile TS file to JS

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FOJFK7pWC08Gq8Tu3MQAJ%2Fimage.png?alt=media&amp;token=016ebe28-fb8b-4cfb-b468-00348cbf5482" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2Fh9HrTxbnpYgXusZTKEuQ%2Fimage.png?alt=media&amp;token=c6f7b0a1-a20f-4093-be83-322852b37af4" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FCyPROczW4JI0xs2P3ySE%2Fimage.png?alt=media&amp;token=b700c3af-fb3c-4f0e-819d-a8f4f8257ad6" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FtyXP6PTqV5GiPm0jQevn%2Fimage.png?alt=media&amp;token=bd3fa27e-03f7-4806-b3c9-88d48b1f0b31" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FyYIyl4jdnSYcozrBac75%2Fimage.png?alt=media&amp;token=ac9a2fec-8f0b-4ac7-8d46-03745c446a73" alt=""><figcaption></figcaption></figure>

## Course Outline

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FACGPsaHqo7NJdMLRdMcz%2Fimage.png?alt=media&amp;token=437e06bf-6622-4d3b-952f-ad18faa5fe1b" alt=""><figcaption></figcaption></figure>

## Getting Started

In Vs code, to start a project, you can just create a .ts file and write some console log statement. To run that (to compile and get js file), run `tsc jsfile.js` and that gives you js file.&#x20;

You can also have an index.html file and inside that, have a script tag that points to js file with defer key like this

```html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Understanding TypeScript</title>
  <script src="app.js" defer></script>
</head>
<body>
  
</body>
</html>
```

Now, to run this and for hot reloading (when you change sometime in ts file), and for it to automatically compile and give new output in browser console, install a dependency called lite-server like this

```javascript
npm install --save-dev lite-server
```

Add start script in package.json like this

```json
{
  "name": "understanding-ts",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "lite-server"
  },
  "author": "Maximilian Schwarzmüller",
  "license": "ISC",
  "devDependencies": {
    "lite-server": "^2.6.1"
  }
}
```

Run \`npm start\` and you should see local server running.

## Typescript Basics and Basic Types

### 💥 💥 Core Types

For both JS and TS

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2F9qOyjtKd7WfRaM5LTFOU%2Fimage.png?alt=media&amp;token=38ed0f28-e9b7-4c2c-b0be-db153f46d237" alt=""><figcaption></figcaption></figure>

Let's look at this code

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FG8gnMrDIXLlc7133Q2SC%2Fimage.png?alt=media&amp;token=36d1eab6-6752-407d-9d21-f5ac0e6a7d3d" alt=""><figcaption></figcaption></figure>

Now, what if number1 is made '5' string instead of number by mistake? We get 52.8 instead of 7.8. To avoid this, we need to specify what type the n1 and n2 should accept. If we say n1 and n2 to be of number type, then we can't pass string but should pass number and we can then avoid that bug.

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FU2raRPBBsrd4mKfruLAh%2Fimage.png?alt=media&amp;token=8389bba8-0327-438f-b6dc-e00e5ea0a62f" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2Fln1wpRiZVtqoIDC4daiV%2Fimage.png?alt=media&amp;token=7e9949a4-7af1-4ea4-b34e-b15d157c6609" alt=""><figcaption></figcaption></figure>

Just be aware of this below issue which happens when you open ts and js files at the same time

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FQ96jHmMu0J3w7MgmI42Z%2Fimage.png?alt=media&amp;token=eb33c5b2-5505-4800-a50f-a1e4b4779470" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
If some error happens during development that is detected by TS, like lets say you are passing a string to number type, then the TS will yell at you and show the error that is happening during compilation, but by default, TS creates corresponding JS file (even if it knows it has an error and DOESN'T stop the compilation and doesn't stop creating JS file).&#x20;

This is the default behaviour and we will later see how to stop creating JS file when a TS compilation error occurs.
{% endhint %}

#### Typescript types vs Javascript types

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2F4W6eEFmmJGx9Lyo8USfC%2Fimage.png?alt=media&amp;token=8f10bada-237b-4988-901f-3c996f9e4d04" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2F6DG6o4D736zVhzllAc15%2Fimage.png?alt=media&amp;token=5b5fbb8d-7ebc-4774-882a-5ef3898ee27a" alt=""><figcaption></figcaption></figure>

### Type Inference

Why we have types only on params and not on definitions like here?

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2Fb7ySrYkocIwdnqgwuhdl%2Fimage.png?alt=media&amp;token=7b4a7c10-a976-4646-8256-7e09185ca8c0" alt=""><figcaption><p>why?</p></figcaption></figure>

Because TS will have something called type inference by default (automatic type detection) . Type inference means, TS auto detects the type as soon as something is assigned to a variable. Let's say

```
const n = 5
```

TS understands that **'n' will be a** **number** as it is assigned with number.  And also, it's a constant, meaning this can't change. We have assigned number 5 to n so it knows 'n' is a number and it's not going to change.

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FtXpBUYBKoeTGp7I0c33X%2Fimage.png?alt=media&amp;token=73de112f-df80-4ac4-9beb-0a86bbd33353" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FiWxlyHANa0saqILrdVr1%2Fimage.png?alt=media&amp;token=13de76c8-29e6-4b45-9406-35da24c1cd75" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FXjMJpYBUFW1lgF2Yb99f%2Fimage.png?alt=media&amp;token=7e75a6fc-b1b6-4455-b58c-fe658ec3f7a0" alt=""><figcaption></figcaption></figure>

### 🚀 Quiz

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FHyTLD2ByxREY01CJlXOf%2Fimage.png?alt=media&amp;token=e7f9ff37-571e-466f-8381-498c0f7b5291" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FGt317joOtq6Ctvhmb0jk%2Fimage.png?alt=media&amp;token=e8c70ee6-36e5-45cf-a6c2-e614bf820f03" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FMRvHqzd4jP8QcRhDZ6dm%2Fimage.png?alt=media&amp;token=83aadaaf-4130-493e-ba0b-ddc978f425af" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FYQ3xdPMmlKDowGYmT5MV%2Fimage.png?alt=media&amp;token=f4175852-a4be-4938-bb82-f26a695cc727" alt=""><figcaption></figcaption></figure>

### 💥 Object type

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2F9RxvGXcrgN7kEpIqq5MB%2Fimage.png?alt=media&amp;token=3b07a08b-b727-485f-9505-a4554b2b7c86" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FnDXd4Cijc9xjhk5Qt92a%2Fimage.png?alt=media&amp;token=44cf5b24-1b56-4b5c-b2b6-c09527ab9889" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FJelN4PKdsZLWbmYUjQix%2Fimage.png?alt=media&amp;token=097b3723-01c4-4737-8a4d-e41ce613af0f" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2Fnq4GUNdVdf3cvnqYLRDf%2Fimage.png?alt=media&amp;token=a382420a-59f2-4fdd-9aec-6e92be25a4a7" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FayX2h7fPAntl4DBUbITM%2Fimage.png?alt=media&amp;token=b8181412-227c-4332-b2fe-ce70c28ba871" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2F4GO4SB1MCKUXplknJIlY%2Fimage.png?alt=media&amp;token=66ab3582-1a7f-42ba-bced-44ba6ea39e34" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2Fk10TIxAINnJhh0jPaYFF%2Fimage.png?alt=media&amp;token=91956af0-5905-4fd5-948c-48f157bd5c8e" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FHdUuPnPzbNwnj0IWEc7I%2Fimage.png?alt=media&amp;token=6993e5ee-893b-4788-8553-1990720f193f" alt=""><figcaption></figcaption></figure>

### 💥 Array type

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2F8TRHLvnLU9lxhbqPVp3Q%2Fimage.png?alt=media&amp;token=26338403-b161-4b7a-aec2-48a36c487e2c" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2Fdt11iBooCYjYSDVGNunp%2Fimage.png?alt=media&amp;token=e3d52ecc-f6c6-43ef-88cd-d7ee67899745" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FlXWB9BDXr9CqEDH0xIWo%2Fimage.png?alt=media&amp;token=5bc2d224-68a1-4109-86b4-928648079842" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FzMNrcRJNECX7QicE7hMx%2Fimage.png?alt=media&amp;token=7a4f84ca-a6b8-4732-9160-c22fe78612dc" alt=""><figcaption></figcaption></figure>

### 🐠 🐠 TS specific types

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2F1t7WUzh3OZKnMgYx2piD%2Fimage.png?alt=media&amp;token=c7d4362f-73b5-4f69-859a-ec4133cf2d1a" alt=""><figcaption></figcaption></figure>

### 🐠 Tuple type

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FCxtGKraxUHfanf3P8Ozt%2Fimage.png?alt=media&amp;token=18f25622-e146-4263-9d9b-fa942d986216" alt=""><figcaption></figcaption></figure>

When we want an array of two elements to have fixed structure, eg. first element should always be string and second should always be number, then we use tuple.

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FJuSiQi7cUlvDHJoW9Dl1%2Fimage.png?alt=media&amp;token=ce458e02-f416-4ce1-931f-f47da304f89b" alt=""><figcaption></figcaption></figure>

Note that the tuple will be a normal array after compiling in JS.&#x20;

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FP1SOjgcNi1v6yQRgx9Py%2Fimage.png?alt=media&amp;token=9f013ba6-5dde-4e65-bc2f-503f7ba0f0c6" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
There is a gotcha or bug I would say for tuple.  See below image.
{% endhint %}

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FaCINYJ2uJyrH2QIEgCGV%2Fimage.png?alt=media&amp;token=618bdf37-d629-4f24-a098-9f496a558fd6" alt=""><figcaption><p>gotcha of tuple</p></figcaption></figure>

### 🐠 Enum type

Let's say we have role key and it should be only one of the defined values

```
Role could be either ADMIN or USER or OWNER
```

In this case we can use tuple.

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FA3NX7mKh9VR5ziet8rUw%2Fimage.png?alt=media&amp;token=2dbfe0db-92ed-4181-9e9e-ded21965ee6a" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FPk2Yv3bn7IVy943QmzBM%2Fimage.png?alt=media&amp;token=efc84502-5412-48df-bc5d-e6ab157883ab" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2F9G4LBjxdjsl3EUjUrFjV%2Fimage.png?alt=media&amp;token=5c7ebb44-a0dd-4f68-b2ff-73f736dde391" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2F2lVhkI2izury05HYPip2%2Fimage.png?alt=media&amp;token=ee50f132-c59e-48b1-a0cb-8415af743d22" alt=""><figcaption></figcaption></figure>

### 🐠 Any type

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FdtMcWZd0RcccZo8LLDxg%2Fimage.png?alt=media&amp;token=4b5ec257-1825-4c24-a274-e8040d3b73fc" alt=""><figcaption></figcaption></figure>

This is the default type. TS will never yell at you when you use any, but the problem is we are not using the power of type script and we might make some mistakes if we use any type. So we need to be careful and use this if it's absolutely necessary.

If you have some variable which you don't know what value will be assigned to it then you can use this, but it's always better to make a proper decision before hand.

### 🐠 Union type

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2F4s1kfbNTbLxye1QLykkr%2Fimage.png?alt=media&amp;token=ea5ae9b6-db4c-4df2-82fc-e584b6857f8a" alt=""><figcaption></figcaption></figure>

one or the other type. In the above example, we can pass either number or a string to input1 and input2. We can have many more unions like `number | string | boolean`

### 🐠 Literal type

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FsHKYj5IfHSky6d7FZHYE%2Fimage.png?alt=media&amp;token=c12b5c87-be4b-4fbd-8734-eb0f8a98d0e6" alt=""><figcaption></figcaption></figure>

### 🐠 Type Aliases / Custom Types

When working with union types, it's annoying always to define like this `string | boolean | number` in multiple places. We might want to create a new type which then stores this union type. We can do this with type aliases.

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2Fgw9kOvUcWDYPKhNmL4Rf%2Fimage.png?alt=media&amp;token=40480d5d-fd7a-4994-8601-b6b5d89ec445" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2F3zBa9n5Raf9MhgVxteAF%2Fimage.png?alt=media&amp;token=55ae7a1b-d5f1-42bd-89d9-ec172280db6b" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2F8vF8J4a5DX0rJRux7EuH%2Fimage.png?alt=media&amp;token=a64883c0-8942-4cb0-9b76-a3b84a936a37" alt=""><figcaption></figcaption></figure>

### 🚀 Quiz

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FRCkVxhZHvzQ1KG91Wcct%2Fimage.png?alt=media&amp;token=64c5d003-6487-4a35-9e75-33c30479e665" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FQCscTqySaNoKr2awB4dO%2Fimage.png?alt=media&amp;token=f40dbec5-99cc-4ecb-8927-5cf9c5a9bd57" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2F4O3e5hfhRnosjAJYvDK8%2Fimage.png?alt=media&amp;token=4f2b2d28-be06-4c64-b1c8-d2545754012c" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FZ8mDRsdvgs2LCjY9WnQP%2Fimage.png?alt=media&amp;token=02380da6-789a-4751-b6f0-59a78ec5afcf" alt=""><figcaption></figcaption></figure>

### 🐠 Function Return types and "void"

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2Fqn0Ml19gV94pikeSSwKe%2Fimage.png?alt=media&amp;token=33a14d2c-b0b2-47ca-ac1b-33fdda3aa063" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FePzRmNv1iwespWef03k9%2Fimage.png?alt=media&amp;token=ec833704-e497-4fb3-b86f-6c08386dd4d5" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FsuqZs0bW5ZCIe81WxsLT%2Fimage.png?alt=media&amp;token=f3c172f9-d21f-4520-84d1-b8a5078ea618" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FGh5kWhhT0XdUI9Om6I1G%2Fimage.png?alt=media&amp;token=93b130c4-cda3-4c90-b243-63e7b6d39372" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FRtRsoepbvfOmp9N7Tgx1%2Fimage.png?alt=media&amp;token=c0319c72-cc72-4382-96d3-b790e9f49ba7" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FHlPXwwMNLi7cxz0fOKtr%2Fimage.png?alt=media&amp;token=ab6f06c5-d491-4e13-8ab9-56a4c82efa71" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FmYCcGiObDGkiIaaDQTwJ%2Fimage.png?alt=media&amp;token=90599c52-1227-49c8-a8ba-3058d7e5cd00" alt=""><figcaption></figcaption></figure>

### 🐠 Functions as Types

Now what if return type is a function itself.&#x20;

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FeXdot7dPwqgfAA5ysPDe%2Fimage.png?alt=media&amp;token=f3435eb3-6717-446d-a4ed-3d752857274d" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2Fi1axQ39rlWXgwppRkyIM%2Fimage.png?alt=media&amp;token=82be2b0b-ff7f-4b61-9b74-f222d1f20c0d" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FS5IpvG2ttoW0ZXot38T7%2Fimage.png?alt=media&amp;token=87b7ee59-f373-46f8-92f1-2679916dd9b1" alt=""><figcaption></figcaption></figure>

So it would be good if we let TS know how the function should look like, for example, how the function args are and what the return type is

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2Fe3mH9iMihkDDG2cramYa%2Fimage.png?alt=media&amp;token=243e5e5c-6fa1-48d2-8821-f5ba31efab85" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FHiZvto7uYsK5ivCJFmcz%2Fimage.png?alt=media&amp;token=6cc68646-f8b8-40b5-8521-1311b00f55e9" alt=""><figcaption></figcaption></figure>

### 🐠 Function Type & Callbacks

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FpaqdGLawxKIYiQHTUP9h%2Fimage.png?alt=media&amp;token=80739702-d83b-4c54-ab35-3c9d29bd2fcb" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FVrRNKJDMEQr0JWvPBTDU%2Fimage.png?alt=media&amp;token=b8896d38-e5dd-406c-bec1-8aba6351152e" alt=""><figcaption></figcaption></figure>

Gotcha (see below image)

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FMmHorTeHNJRcr2j5ZGlA%2Fimage.png?alt=media&amp;token=25d020ea-9a97-46e3-8e63-33be3f040512" alt=""><figcaption></figcaption></figure>

### 🚀 Quiz

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FELt88HM0blvSrGo5Lwpb%2Fimage.png?alt=media&amp;token=bfd5b0cd-7754-4856-bf60-5ea84580ca42" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FMAzXlUF3cAekGpZ675Oh%2Fimage.png?alt=media&amp;token=9bff166e-e93e-439e-b48e-06a8b9724ad8" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2Fn8shSl9gY9XWZbCdc0tQ%2Fimage.png?alt=media&amp;token=73113653-4005-4c2f-88b7-dd06524193e6" alt=""><figcaption></figcaption></figure>

### 🐠 Unknown type

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FAzJSwfnvWChSarWx61IT%2Fimage.png?alt=media&amp;token=d0e7ad3b-0ca7-4dd2-9de8-43308daaefe0" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2FRLu0QHmpa216JVoXTtle%2Fimage.png?alt=media&amp;token=7a80f2d7-1e0c-4a6c-8a51-1eef2e05e035" alt=""><figcaption></figcaption></figure>

unknown should also not be used always but is better than any type

### 🐠 Never type

never is another type that functions can return.&#x20;

<figure><img src="https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MVEiPUp08kYt33g51v7%2Fuploads%2Fi8GDLcJh8tB0n6lpf9jn%2Fimage.png?alt=media&amp;token=1f39afba-b58c-4bc7-a313-ccdf27d99bbc" alt=""><figcaption></figcaption></figure>

### Useful resources

{% embed url="<https://www.typescriptlang.org/docs/handbook/2/everyday-types.html>" %}
