Nin*_*liu 7 typescript typeguards
此代码无法编译:
const nodeIsUseless = (node: unknown) =>
node !== null &&
typeof node === "object" &&
"type" in node &&
typeof node.type === "string" &&
node.type === "JSXText";
Run Code Online (Sandbox Code Playgroud)
因为在最后两行:
类型“对象”上不存在属性“类型”。(2339)
...我自己可以理解,但我不明白为什么"type" in node检查后, TS 推断node仍然是 typeobject而不是 type { type: unknown; [key: string]: unknown },这不会触发 error。
| 归档时间: |
|
| 查看次数: |
7364 次 |
| 最近记录: |