# She's my type : Typescript - JS with Trust Issues!

Yeah… that title was pure attention-grab.  
Let’s move to the real thing now..

So , In this blog .. **We're going to see the basics of TypeScript** , which might be a quick starter for most of you guyz. I'm presuming that you already know enough of JavaScript to be the part of this discussion that we're going to have..

So , grab ur popcorns.. volume up the music (if you're listening ) and Let's just get started!!

* * *

## What is TypeScript

Typescript is built on top of JavaScript , to make it better . Think of it as JavaScript with the ability to do **type checks** .

Javascript is dynamically types.. which means you don't declare what type of data a variable holds. This gives flexibility but can lead to runtime errors that are hard to catch. TypeScript adds types , helping you catch errors **before** you even run your code.

In simple words : **TYPESCRIPT = JS + Type Safety**

## Installation

To use typescript , we first need to install that.

And there's always a best practice to do certain things ..  
Of course you could simply install it using

```shell
npm install -g typescript
```

and use it globally throughout your dev journey ,

but what if your friend wants to work on your code and has not installed typescript ? That's pretty **inconvenient** .

Thus it's always better to do

```shell
npm install typescript -D
```

to install that as a dev dependency in your project.., even if you have installed it globally.

Which ensures that anyone cloning your project can install the exact version of TS your project needs.

* * *

### BASIC TYPES

Now let's dive into the basics of TypeScript. Here are some fundamental types you'll use regularly :

### 1\. String

```typescript
let firstName : string = "Samiksha" ;
// firstname = 123 ❌; Error - can't assign number to string  
```

### 2\. Number

```typescript
let age: number = 25;
let price: number = 99.99;
```

### 3\. Boolean

```typescript
let isLoggedIn: boolean = true;
let isCompleted: boolean = false;
```

### 4\. Array

```typescript
let numbers: number[] = [1, 2, 3, 4];
let names: Array<string> = ["Alice", "Bob", "Charlie"]; // alternative syntax
```

### 5\. Tuple

Fixed-length array with specific types at each position:

```typescript
let user: [string, number] = ["Alice", 25];
// user = [25, "Alice"]; ❌ Error - order matters
```

### 6\. Enum

Named constants that make code more readable:

```typescript
enum Color {
  Red,
  Green,
  Blue
}
let myColor: Color = Color.Green;
```

### 7\. Any

The escape hatch.. use sparingly:

```typescript
let notSure: any = 4;
notSure = "maybe a string";
notSure = false; // anything goes
```

### 8\. Void

Absence of any type, commonly used for functions that don't return anything:

```typescript
function logMessage(message: string): void {
  console.log(message);
}
```

### 9\. Null and Undefined

```typescript
let u: undefined = undefined;
let n: null = null;
```

### 10\. Object

```typescript
let person: { name: string; age: number } = {
  name: "Jack",
  age: 30
};
```

* * *

## Type Inference

The cool thing is.. you don't always need to explicitly write types. TypeScript infers them :

```javascript
let myname = "Amara";  // TS knows it's a string.. 
// myname = 45; will give error that can't assign number
```

## Interfaces

Interfaces define the shape of an object like a contract :

```typescript
interface Person {
    name : string ; 
    age : number ; 
    email? : string ; // optional property
} 

function greet(person : Person) : string {
    return `Hello, ${person.name}` ; 
}

let user: Person = {
    name : "Alok" ; 
    age : 35;
};

console.log(greet(user)); // Hello Alok
```

## Functions with Types

```typescript
function add(x:number  , y:number) : number {
    return x+y;
}

// optional parameters.. 
function buildName (firstName: string , lastname? : string) : string {
    if (lastname) {
        return `${firsname} ${lastname}`;
    }
return firstName ;
}
```

## Compiling TypeScript

TypeScript code can't run directly in browsers.. You need to compile it to JavaScript ;

```c
#create a tsconfig.json file

npx tsc --init

#compile all typescript files.. 
npx tsc 

# watch mode.. (autocompiles on changes)
npx tsc --watch
```

* * *

## Why TypeScript Matters

**1\. Catch errors early** - before runtime

**2\. Better IDE support** - autocomplete , navigation , refactoring.. etc..

**3\. Self-documenting code** - types act as documentation

**4\. Safer refactoring** - change code with confidence..

**5\. Better team collaboration** - and obv better collab.

## Conclusion

Typescript isn't just a trend - it's becoming the standard .. for large-scale JavaScript applications. It helps you write cleaner , safer , and more maintainable code. And the best part? **You can adopt it gradually in your existing JS projects**.

So go ahead, give TS a try. **She might just be your type**...

Happy coding!!
