typescript - 用于识别总是评估的 if 分支的编译器规则
问题描述
在这段代码中:
class Abc {
field1: string;
constructor(data: string) {
if (true) {
this.field1 = "";
}
}
}
没有编译器错误。
但是如果我将if
条件中的表达式更改为data !== null
,则会出现编译器错误:Property 'field1' has no initializer and is not definitely assigned in the constructor
。但是从编译器的角度来看,datatype ofstring
永远不应该是 null,那只能是string | null
. 为什么编译器会给出这个错误?
解决方案
推荐阅读
- python - 类型错误:预期的字符串
- mongoose - 你如何在 Jest 中模拟 Mongoose 函数?
- powershell - 如何使用powershell将文件从驱动器的根目录复制到另一个文件夹
- javascript - 使用 php 获取 ajax 帖子
- javascript - 如何为 npm 脚本提供默认参数值?
- php - 如何将完整的 html 代码添加到 PHP 数组中并使用 array_rand 函数回显数组
- python - 如何使用线性回归预测 Python 中的 Na
- java - Android - 没有第三方库的堆栈 CardView?
- unity3d - Unity 中搅拌机罐模型的碰撞器
- python - 使用条件创建新实例