Predict Union Narrowing 输出
预测输出 简单
题目
什么做 这个 code print?
代码
function process(value: string | number) {
if (typeof value === "string") {
console.log(value.toUpperCase());
} else {
console.log(value * 2);
}
}
process("hi");
process(5);
提示
首先 call 通过字符串, 其次通过数字.
解释
当处理("hi") 是 called, typeof 值是 "string", 所以它 logs "hi".toUpperCase() = "HI". 当处理(5) 是 called, typeof 值是 "number", 所以它 logs 5 * 2 = 10. 输出是 HI on one 行 and 10 on 下一个.
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。