> For the complete documentation index, see [llms.txt](https://xiaoyang-liu.gitbook.io/programming-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xiaoyang-liu.gitbook.io/programming-notes/web-development/typescript-handbook/basic-types.md).

# Basic Types

## Boolean

```typescript
let isDone: boolean = false;
```

## Number

```typescript
let decimal: number = 6;
let hex: number = 0xf00d;
let big: bigint = 100n;
```

## String

```typescript
let fullName: string = "Xiaoyang Liu";
let sentence: string = `Hello, my name is ${fullName}.`;
```

## Array

```typescript
let list: number[] = [1, 2, 3];
let genericList: Array<number> = [1, 2, 3];
```

## Tuple

Tuple types allow you to epxress an array with a fixed number of elements whose types are known, but need not be the same.

```typescript
let x: [string, number] = ["hello", 1];

// Error: Property 'substring' does not exist on type 'number'.
x[1].substring(1);

// Error: Tuple type '[string, number]' of length '2' has no element at index '3'.
x[3] = "world";
```

## Enum

An enum is a way of giving more friendly names to sets of numeric values. By default, enums begin numbering starting at 0.

```typescript
enum Color {
  Red,
  Green,
  Blue,
}

let c: Color = Color.Green;
let colorName: string = Color[2]; // map value to name

enum Color1 {
  Red = 1,
  Green,
  Blue,
}

enum Color2 {
  Red = 1,
  Green = 10,
  Blue = 100,
}
```

## Unknown

Values with `unknown` type may come from dynamic content (from user) or we may want to intentionally accept all values in our API.

```typescript
let notSure: unknown = 4;
notSure = false;
```

We could narrow the value with `unknown` type down with `typeof` checks, comparison checks, or other advanced type guards.

```typescript
declare const maybe: unknown;

if (maybe === true) {
  // TypeScript knows that maybe is a boolean now
  const aBoolean: boolean = maybe;
  // So, it cannot be a string
  const aString: string = maybe; // Error
}

if (typeof maybe === "string") {

}
```

## Any

In some situations, not all type information is available or its declaration would take an inappropriate amount of effort. In these cases, we might want to opt-out of type checking. To do so, we label these values with the `any` type:

```typescript
declare function getValue(key: string): any;

// Return value of the function is not checked
const str: string = getValue("myString");
```

Type safety is one of the main motivations for using TypeScript and you should try to avoid using any when not necessary.

## Void

`void` means the absence of having any type at all. You may commonly see this as the return type of functions that do not return a value:

```typescript
function warnUser(): void {
  console.log("This is a message");
}
```

## Null and Undefined

In TypeScript, both `undefined` and `null` actually have their types named `undefined` and `null` respectively.

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

By default `null` and `undefined` are subtypes of all other types. That means you can assign `null` and `undefined` to something like `number`.

However, when using the `--strictNullChecks` flag, `null` and `undefined` are only assignable to `unknown`, `any` and their respective types (the one exception being that `undefined` is also assignable to `void`).

## Never

The `never` type represents the type of values that never occur. The `never` type is a subtype of, and assignable to, every type. However, no type is assignable to `never`.

```typescript
function error(message: string): never {
  throw new Error(message);
}

// Function returning never must not have a reachable end point
function infiniteLoop(): never {
  while (true) {}
}
```

## Object

`object` is a type that represents the non-primitive type. With `object` type, APIs like `Object.create` can be better represented.

```typescript
declare function create(o: object | null): void;

create({ prop: 0 });
create(null);
```

## Type assertions

Type assertions are a way to tell the compiler "trust me, I know what I’m doing."

* `as` syntax

```typescript
let someValue: unknown = "this is a string";

let strLength: number = (someValue as string).length;
```

* "angle-bracket" syntax

```typescript
let someValue: unknown = "this is a string";

let strLength: number = (<string>someValue).length;
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://xiaoyang-liu.gitbook.io/programming-notes/web-development/typescript-handbook/basic-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.
