Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typescript手册-类型别名type #20

Open
Pasoul opened this issue Mar 15, 2019 · 0 comments
Open

typescript手册-类型别名type #20

Pasoul opened this issue Mar 15, 2019 · 0 comments

Comments

@Pasoul
Copy link
Owner

Pasoul commented Mar 15, 2019

类型别名通常用来给一个类型取新名字。本质上不会创建一个新的类型,而是对原有类型的引用。

语法

type 新名字 = 类型

简单的例子

type Name = string;
// NameResolver表示这是一个函数类型,并且函数的返回值类型是string
type NameResolver = () => string;
type NameOrResolver = Name | NameResolver;
function getName(n: NameOrResolver): Name {
    if (typeof n === 'string') {
        return n;
    }
    else {
        return n();
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant