# 1.1: Types

TypeScript, just like JavaScript, accepts different units of data. Things like strings, numbers, booleans...stuff you're all used to by now. The difference now is we need to be explicit in what unit of data is going into the variable.

Setting the type is a snap in TypeScript. Just place the type after a `:` and you're good to go! Let's take a look at the different types.

## BOOLEANS

At this point, we know that booleans are simple true/false statements:

```typescript
let danzigRules: boolean = true;
```

## STRINGS

TypeScript uses single or double quotes for string data, just like in JavaScript:

```typescript
let bestMusician: string = 'Danzig';
let favoriteColor: string = "Grey";
```

Template strings may also be used in Typescript:

```typescript
let name: string = `Cybernetic Ghost of Christmas Past from the Future`;
let greeting: string = `Hello, I am the ${name}.`
```

## NUMBERS

Numbers in TypeScript are floating value points, which are numbers with fractional parts (for example, 3.65). TypeScript also accepts hexadecimal, binary, and octal literals:

```typescript
let integer: number = 38;
let decimal: number = 45.67;
let hex: number = 0xf00d;
let binary: number = 0b1010;
let octal: number = 0o744;
```

## ARRAYS

Array types can be written in two different ways:

```typescript
let bestRolePlayingGames: string[] = ['PathFinder', 'StarFinder', 'Orpheus'];
```

or as an generic array type:

```typescript
let tmnt: Array<string> = ['Leonardo', 'Donetello', 'Raphael', 'Michelangelo'];
```

## TUPLES

These are arrays with with mixed types and a limited number of items. Tuples have to be a string and a number:

```typescript
let nena: [number, string] = [99, 'Red Luftballons'];
```

Please note, order is important! So if you indicate numbers are first in the array, then they **HAVE** to be first or you will get an error.

## ENUMS

Enums are a TypeScript feature that makes numbers more expressive. By default, enums start at number 0:

```typescript
enum Color {
    Black,
    Grey = 200, 
    Pink
}
// Black equals 0, Grey equals 200, and Pink equals 201.  If you want Pink to equal 2,
// then you would need to manually set it.
```

## ANY

Occasionally, we may not know what type of variables when we are creating our applications. We can use `any` to allow for any type of data to get set in the variable.

```typescript
let item: any = 'Bag of Holding';
item = 4;
//if item wasn't set to any, then setting item to a number would have
//thrown an error
```

## VOID

Void could almost be considered the opposite of any. We see `void` used as the return type of functions that do not return a value:

```typescript
function lastCaress(): void {
    console.log('I got something to say!');
}
```

Declaring `void` on a variable is not the most useful practice, because we can only assign `undefined` or `null` to them. In fact, `undefined` and `null` are there own types:

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

## NEVER

Never represents a value that will never occurs. We usually see `never` with functions that have an unreachable end point:

```typescript
function error(): never {
    throw new Error('Just what do you think you\'re doing Dave?');
}
```

## FUNCTION TYPES

Types can be added to the arguments as well as the return type of the function:

```typescript
function subtract(number1: number, number2: number): number {
    return number1 - number2;
}
```

We can use arrow functions as well:

```typescript
let newSubtract = (number1, number2) => number;
newSubtract = subtract;
console.log(newSubtract(6, 3));
```

## TYPE INFERENCE

TypeScript can use type inference to provide information on the type when there is no explicit annotation. For example:

```typescript
let marathonMan = 'Chris';
```

TypeScript now assumes that the variable `marathonMan` is of the type `string`. If we tried to set it to anything else, we would get an error. This works, but since we are writting in TypeScript for Anuglar, let's get in the habit of being explicit with our types.

*adapted from TypeScript Documentation and Understanding TypeScript by Maximilian Schwarzmüller*

## NEXT SECTION

[1.2: Classes](https://github.com/eleven-fifty-academy/javascript-401-angular/tree/880f3893d0b7d596934e8bb5fe89617cd71465f1/docs/PartOne/PartOne/1.2.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://eleven-fifty-academy.gitbook.io/javascript-401-angular/part-1-typescript-overview/1.1-types.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
