const 字面量类型
预测输出 简单
题目
这段代码打印什么?
代码
const x = "hello" as const;
console.log(typeof x);
提示
as const 使类型为字面量 "hello",但 typeof 仍为 string。
解释
用 as const,x 的类型为字面量 "hello"(非通用 string)。但运行时 typeof "hello" 仍为 "string"。字面量类型仅编译时概念。
相关资源
相关课程
- 类型推断
学习类型推断。
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。