Using Typescript types
Tags: typescript, codenewbie, javascript
Published: 2020-03-31 00:00:00

Typescript comes with his own types and each one is part of typescript, not javascript.

Typescript will provide these types as part of our development process and notify the compiler of the data type used and force us to implement properly.

## Tuples

Allows set a variable or array with a set of values of different data types.

tuples are very useful to don't create a type of dummy class or type just for a store 2 types in a variable.

```javascript
let codeRole: [number, string];

codeRole = [1504, "ADMIN"];

let sickTypePriority: [number, boolean, string][];

sickTypePriority = [
    [2033, true, "Cancer"],
    [2039, false, "Flu"]
];


```

## Enums

The enum allows removing the magic numbers for a human-readable value and can be used as a type.

The enum is declared similar to an object with the enum keyword.

```javascript
enum VatType {
    "NoTax" = 0.0,
    "SpainTax" = 21.0,
    "Private" = 12.0
}

```

The enums can be used as a type in the variables or props.

```javascript
let payment: {
    amount: number;
    tax: VatType;
} = {
    amount: 1400,
    tax: VatType.Private
};


```

## Any

Any type is a flexible type to a specific type assignment, as a javascript and loses all compiler and checks.

The variables with any type can store all types without a compiler error.

```javascript
let colors: any;  

colors = 1;
colors = "hello"
colors = []
colors = true;


```

## Union Types

the union types allow combining 2 or more types for our variable or parameter with the compiler check restriction.

For example, we want to store the security key and it can be a number or string.

The union types can be used in function parameters.

```javascript
Let securityKey : string | number; 
securityKey = "DEFAULT TOKEN";
securityKey = 213235456;

function AddToken(token: number | string) {  
    console.log(token); 
}

```

## Literal Types

The literal types can be used when we know accepted values that can be stored in a variable, similar to a union type.

We can use it to force specific values.

```javascript
let currencyType: 'USD' | 'EUR' | 'DO'

currencyType = "DO";
currencyType = "EUR";


```

## Type Aliases

Type aliases are used to create your own type using the type keyword.

Using the union type you can combine all types instead create a dummy class.

```javascript
type BankAccount = { name: string; amount: number };

class Bank {
    process(account: BankAccount) {
        account.name = "Saving";
        account.amount = 1500;
    }
}


```

## unknow

the unknown type allows store any data but with the restriction when it is initialized.

unknown doesn't allow us to assign his type to another data type instead we are sure of the conversion.

```
let token: unknown;
let key: string;

token = "asdasdas";
key = "hello world";

key = token;

error TS2322: Type 'unknown' is not assignable to type 'string'.

key = token;

```
## That's it!

Hopefully, that will give you a bit of help using the Typescript types. If you enjoyed this post, share it.

Photo by <a href="https://unsplash.com/@deonblack?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Deon Black</a> on <a href="https://unsplash.com/s/photos/types?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>