概念: Variance
概念检查 中等
题目
如何做 TypeScript handle variance for 数组类型?
提示
Think 关于 whether 数组的 dogs 能是 used 哪里数组的 animals 是 expected.
解释
TypeScript 数组是 covariant: 如果 Dog extends Animal, 然后 Dog[] 是 assignable 到 Animal[]. 这个是 technically unsound (你能 推送 Cat 到 Dog[] 输入 as Animal[]), 但它 matches JavaScript's 运行时 behavior and 是 practical. 函数参数是 bivariant 通过默认 (strictFunctionTypes 制作它们 contravariant).
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。