Discover the magic of the TypeScript type system and how it works its magic on your code
Tags: typescript, javascript, webdev, beginners
Published: 2022-12-30 10:19:24

I've been diving into TypeScript for the past three days and one thing I've learned is that its type system is like a secret weapon🤯. By annotating variables, functions, and objects with their data types, you can catch errors before they even have a chance to cause trouble and make your code super reliable and easy to keep up-to-date.

In this article, I'm going to geek out about all the amazing features of TypeScript's type system. We'll dive into topics like declaring types, using type inference, and writing code that's super safe and secure thanks to its use of types.

### Declaring Types
In TypeScript, we can declare the type of a variable by using a colon followed by the type name. For example:

```typescript 
let myNumber: number = 5;
let myString: string = 'Hello, world!';
let myBoolean: boolean = true;
```
In this example, we have defined three variables: `myNumber`, `myString`, and `myBoolean`. We have used the colon syntax to specify the types of these variables, showing that `myNumber` is a number, `myString` is a string, and `myBoolean` is a boolean.

We can also declare the type of an array using the Array type and indicating the type of the elements within brackets. For example:

```typescript 
let myNumbers: Array<number> = [1, 2, 3];
let myStrings: string[] = ['a', 'b', 'c'];
```

In these examples, we have declared two arrays: myNumbers and `myStrings`. `myNumbers` is an array of numbers, and myStrings is an array of strings.

### Type Inference
Besides explicitly stating the type of a variable, TypeScript also has a feature called type inference which allows the compiler to automatically determine the type of a variable based on the value assigned to it. so cool 😎 right! Let's see an example
```typescript
 let name = 'John'; // TypeScript infers the type to be string
let age = 30; // TypeScript infers the type to be number
let isCool = true; // TypeScript infers the type to be boolean
```
In this case, we haven't explicitly specified the types of the name, age, and isCool variables, but TypeScript is able to deduce their types based on the values they are assigned

Type inference is a handy feature that can save us from having to write out explicit type annotations in many situations. However, it's always a good idea to use explicit type annotations whenever possible, as they make the code more clear and easier to comprehend.

### Type-Safe Code
One of the main advantages of using TypeScript is that it helps youwrite type-safe code. This means that the type system is used to make sure that variables are only used in ways that are consistent with their types.
Let's see an example:
```typescript 
let myNumber = 42;
myNumber = 'hello'; // error: cannot assign a string to a number
```
In this case, the type system will prevent a string from being assigned to a variable that has been declared as a number. This helps catch mistakes early on and can save time and effort in debugging.

Another way to write type-safe code in TypeScript is to use type guards. A type guard is a way to narrow the type of a value based on certain conditions. For example:

```typescript 
function isString(value: any): value is string {
  return typeof value === 'string';
}

let myValue: any = 'hello';
if (isString(myValue)) {
  console.log(myValue.toUpperCase()); // 'HELLO'
}
```
In this example, the isString function is used to determine if the value of myValue is a string. If it is, the toUpperCase method can be safely called on it. Otherwise, the code inside the if block will not be executed.

### Conclusion
TypeScript's type system is like a superhero for your code! It helps you write code that's more dependable and easier to maintain. By specifying types, using type inference, and writing type-safe code, you get all the perks of static typing while still using the sweet, sweet syntax of JavaScript.

      Thanks 👍 for reading