xcatliu / typescript-tutorial

TypeScript 入门教程
https://ts.xcatliu.com
10.44k stars 1.33k forks source link

联合类型 #155

Open xcatliu opened 4 years ago

xcatliu commented 4 years ago

https://ts.xcatliu.com/basics/union-types.html

pidehen23 commented 4 years ago

啦啦啦

hhj321 commented 4 years ago

oh my god!

cody1991 commented 4 years ago

god!

pidehen23 commented 4 years ago

真好啊

honghu1994 commented 4 years ago

666

AzureSoar commented 4 years ago

最后那个例子被赋值后还是联合类型吗?还是变成了其中一个类型

xcatliu commented 4 years ago

@AzureSoar 最后那个例子被赋值后还是联合类型吗?还是变成了其中一个类型

还是联合类型。

SharkBaby commented 3 years ago

多谢大佬的无私

lzzqwe commented 3 years ago

卧槽

lzzqwe commented 3 years ago

@hhj321 oh my god!

这手速只能是的撒

yomua commented 3 years ago

交叉类型是我没注意到吗。。

lzzqwe commented 3 years ago

------------------ 原始邮件 ------------------ 发件人: "Yomua"<notifications@github.com>; 发送时间: 2020年12月23日(星期三) 下午5:20 收件人: "xcatliu/typescript-tutorial"<typescript-tutorial@noreply.github.com>; 抄送: "李重泽"<352840359@qq.com>; "Comment"<comment@noreply.github.com>; 主题: Re: [xcatliu/typescript-tutorial] 联合类型 (#155)

交叉类型是我没注意到吗。。

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

Amigosen commented 3 years ago

真棒

Amigosen commented 3 years ago

针不戳

yclgkd commented 3 years ago

6

ggg1it commented 3 years ago

function getString(something: string | number): number { return something.toString(); } 这样写为什么会报错呢、

yclgkd commented 3 years ago

function getString(something: string | number): number { return something.toString(); } 这样写为什么会报错呢、

传入字符串返回字符串,传入数字返回数字

function getString(something: string | number): string | number {
    return something.toString();
}

重载

或者这么写

function getString(something: string): string;
function getString(something: number): number;
function getString(something: string | number): string | number {
    return something.toString();
}
weitao-Li commented 3 years ago

确实是简单易懂。联合声明 | easyUnderstand