Predict typeof Guard 输出
预测输出 简单
题目
什么做 这个 code print?
代码
function describe(x: string | number | boolean) {
if (typeof x === "string") return "text:" + x;
if (typeof x === "number") return "num:" + x;
return "bool:" + x;
}
console.log(describe("hi"));
console.log(describe(42));
console.log(describe(true));
提示
每个 typeof 检查 narrows 类型 and 返回不同 prefix.
解释
描述("hi") — typeof 是 "string", 返回 "text:hi". 描述(42) — typeof 是 "number", 返回 "num:42". 描述(真) — typeof 是 "boolean", 返回 "bool:true". 输出: 文本:hi, 编号:42, bool:真 on separate 行.
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。