typescript-tutorial icon indicating copy to clipboard operation
typescript-tutorial copied to clipboard

联合类型

Open xcatliu opened this issue 4 years ago • 18 comments

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

xcatliu avatar Jun 08 '20 12:06 xcatliu

啦啦啦

pidehen23 avatar Sep 06 '20 05:09 pidehen23

oh my god!

hhj321 avatar Sep 28 '20 09:09 hhj321

god!

cody1991 avatar Oct 01 '20 03:10 cody1991

真好啊

pidehen23 avatar Oct 01 '20 04:10 pidehen23

666

honghu1994 avatar Oct 23 '20 03:10 honghu1994

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

AzureSoar avatar Nov 03 '20 09:11 AzureSoar

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

还是联合类型。

xcatliu avatar Nov 03 '20 10:11 xcatliu

多谢大佬的无私

SharkBaby avatar Dec 03 '20 02:12 SharkBaby

卧槽

lzzqwe avatar Dec 15 '20 01:12 lzzqwe

@hhj321 oh my god!

这手速只能是的撒

lzzqwe avatar Dec 15 '20 01:12 lzzqwe

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

yomua avatar Dec 23 '20 09:12 yomua

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

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

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

lzzqwe avatar Dec 23 '20 09:12 lzzqwe

真棒

Amigosen avatar Jan 06 '21 08:01 Amigosen

针不戳

Amigosen avatar Jan 06 '21 08:01 Amigosen

6

yclgkd avatar Jan 28 '21 03:01 yclgkd

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

ggg1it avatar Feb 01 '21 05:02 ggg1it

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();
}

yclgkd avatar Feb 01 '21 06:02 yclgkd

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

weitao-Li avatar Feb 04 '21 08:02 weitao-Li