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

联合类型 #155

Open
xcatliu opened this issue Jun 8, 2020 · 18 comments
Open

联合类型 #155

xcatliu opened this issue Jun 8, 2020 · 18 comments

Comments

@xcatliu
Copy link
Owner

xcatliu commented Jun 8, 2020

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

@pidehen23
Copy link

啦啦啦

@hhj321
Copy link

hhj321 commented Sep 28, 2020

oh my god!

@cody1991
Copy link

cody1991 commented Oct 1, 2020

god!

@pidehen23
Copy link

真好啊

@honghu1994
Copy link

666

@AzureSoar
Copy link

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

@xcatliu
Copy link
Owner Author

xcatliu commented Nov 3, 2020

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

还是联合类型。

@SharkBaby
Copy link

SharkBaby commented Dec 3, 2020

多谢大佬的无私

@lzzqwe
Copy link

lzzqwe commented Dec 15, 2020

卧槽

@lzzqwe
Copy link

lzzqwe commented Dec 15, 2020

@hhj321
oh my god!

这手速只能是的撒

@yomua
Copy link

yomua commented Dec 23, 2020

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

@lzzqwe
Copy link

lzzqwe commented Dec 23, 2020 via email

@Amigosen
Copy link

Amigosen commented Jan 6, 2021

真棒

@Amigosen
Copy link

Amigosen commented Jan 6, 2021

针不戳

@yclgkd
Copy link
Contributor

yclgkd commented Jan 28, 2021

6

@ggg1it
Copy link

ggg1it commented Feb 1, 2021

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

@yclgkd
Copy link
Contributor

yclgkd commented Feb 1, 2021

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
Copy link

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

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