Predict Generic Inference 输出
预测输出 中等
题目
什么做 这个 code print?
代码
function identity<T>(value: T): T {
return value;
}
console.log(identity(42));
console.log(identity("hello"));
console.log(identity([1, 2, 3]));
提示
identity 只是返回 whatever 你通过 in. TypeScript infers T 从实参.
解释
identity(42) — T 是 inferred as 数字, 返回 42. identity("hello") — T 是字符串, 返回 "hello". identity([1, 2, 3]) — T 是数字[], 返回数组. console.log 的数组 prints comma-separated 值. 输出: 42, hello, 1,2,3.
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。