鸟语天空
TypeScript联合类型
post by:追风剑情 2018-11-15 17:57

示例

/**
//不要为仅在某个位置上的参数类型不同的情况下定义重载
function fn(x: number) {
	return "number类型"+x;
}
function fn(x: string) {
	return "string类型"+x;
}
*/

//应该尽可能地使用联合类型
function fn(x: number|string) {
	return "联合类型: "+x+"("+typeof(x)+")";
}

console.log(fn(5));
console.log(fn("ssss"));


运行测试

1111111.png

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容