> For the complete documentation index, see [llms.txt](https://lydiandylin.gitbook.io/vlang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lydiandylin.gitbook.io/vlang/mu-lu/type_alias.md).

# 类型别名

\##类型别名(type alias)

可以在某一个类型或枚举的基础上定义类型别名，使用上完全一样。

#### 基于基本类型

```v
type Myint = int

type Myf32 = f32

type Myf64 = f64

fn main() {
	i := Myint(10)
	println(i + 100 == 110)
	f := Myf64(1.0)
	println(f + 3.14 == 4.14)
}
```

#### 基于结构体类型

```v
module main

struct Human {
	name string
}

pub fn (h Human) str() string {
	return 'Human: $h.name'
}

type Person = Human

pub fn (h Person) str() string {
	return 'Person: $h.name'
}

fn main() {
	p := Person{'Bilbo'}
	p2 := Human{'jack'}
	println(p)
	println(p2)
}
```

#### 基于枚举

```v
enum MyEnum {
	something
	another
	third
}

type MyEnumAlias = MyEnum

fn main() {
	x := MyEnum.something
	println(x)
	a := MyEnumAlias.something
	println(a)
	println(MyEnum.third)
	println(MyEnumAlias.third)
	println(int(MyEnum.third))
	println(int(MyEnumAlias.third))
	println(MyEnum.third == MyEnumAlias.third)
}
```

\###类型别名方法

可以像结构体那样，给类型别名添加方法：

```v
module main

fn main() {
	i := Myint(11)
	println(i.str())
}

type Myint = int

pub fn (m Myint) str() string { // 类型别名的方法
	return 'from myint'
}
```


---

# 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://lydiandylin.gitbook.io/vlang/mu-lu/type_alias.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.
