TypeScript 入门教程
  • TypeScript 入门教程
  • 进阶
    • 类与接口
    • 类
    • 声明合并
    • 装饰器
    • 枚举
    • 扩展阅读
    • 泛型
    • 字符串字面量类型
    • 元组
    • 类型别名
  • 基础
    • 任意值
    • 内置对象
    • 声明文件
    • 原始数据类型
    • 类型断言
    • 类型推论
    • 数组的类型
    • 函数的类型
    • 对象的类型——接口
    • 联合类型
  • 工程
    • 编译选项
    • 代码检查
  • 简介
    • 安装 TypeScript
    • Hello TypeScript
    • 什么是 TypeScript
    • why-typescript
  • 感谢
Powered by GitBook
On this page
  • 简单的例子
  • 参考

Was this helpful?

  1. 进阶

类型别名

Previous元组Next基础

Last updated 4 years ago

Was this helpful?

类型别名用来给一个类型起个新名字。

简单的例子

type Name = string;
type NameResolver = () => string;
type NameOrResolver = Name | NameResolver;
function getName(n: NameOrResolver): Name {
    if (typeof n === 'string') {
        return n;
    } else {
        return n();
    }
}

上例中,我们使用 type 创建类型别名。

类型别名常用于联合类型。

参考

  • ()

Advanced Types # Type Aliases
中文版