> For the complete documentation index, see [llms.txt](https://www.monosor.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.monosor.dev/courses/typescript-101/literal-types.md).

# Literal Types

นอกจาก `interface` แล้ว เรายังสามารถสร้าง Type ใหม่ด้วยคีย์เวิร์ด `type` ได้เช่นกัน โดยความแตกต่างคีอ `interface` ใช้สร้าง Type สำหรับ Object หรือ Class แต่ `type` จะสามารถกำหนดเป็นค่าแบบตรงตัว (Literal) ได้เลย

สรุปง่ายๆ ว่ามันคือการประกาศตัวแปรให้เป็น `type` เพื่อที่จะนำไปใช้ต่อ

```typescript
type Hello = string           // ให้ type Hello เป็น string
const a: Hello = "World"      // ใช้งาน type Hello

type Hi = "Hi"                // ให้ type Hi เป็น "Hi" แบบตรงตัว
const b: Hi = "Hi"
const c: Hi = "Hello"         // Error : เพราะค่าต้องเป็น "Hi" แบบตรงตัวเท่านั้น
```

ประโยชน์ของ Literal Type คือเราสามารถใช้ Union `|` ได้ เพื่อกำหนดให้ Type เป็นค่าใดค่าหนึ่ง

```typescript
type Hi = "Hi" | "Hello"     //  type Hi เป็นคำว่า "Hi" หรือ "Hello" ก็ได้

type Dice =                  // type Dice ต้องเป็นเลข 1-6
  | 1                        // เขียนแบบหลายบรรทัด
  | 2 
  | 3 
  | 4 
  | 5 
  | 6
```

หรือจะใช้ Union กับตัว Type อื่นๆ ก็ยังได้

```typescript
type NotNumber = string | boolean | array 
// ต้องมี type string หรือ boolean หรือ array
```

## Literal Narrowing

การต้ังค่าคงที่ `const` นั้นจะทำให้ Type เป็นแบบ Literal ส่วนการตั้งตัวแปรแบบ `let` หรือ `var` นั้นจะเป็น Type แบบปกติ ซึ่งเปลี่ยนค่าได้ เช่น

```typescript
const x = "ABC"    // x มี Type เป็น "ABC" ซึ่งเป็น Literal เพราะใช้ const
x = "DEF"          // ❌ เปลี่ยนค่า x ไม่ได้ เพราะต้องเป็น "ABC" เสมอ

let y = "XYZ"      // y มี Type เป็น string เพราะเปลี่ยนเป็นค่าอื่นได้เนื่องจากใช้ let
y = "ABC"          // ✅ เปลี่ยนค่า y ได้ เพราะยังเป็น string อยู่
```


---

# 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, and the optional `goal` query parameter:

```
GET https://www.monosor.dev/courses/typescript-101/literal-types.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
