typescript - 输入默认值
问题描述
有什么方法可以创建将使用默认值声明的类型。而是编写相同的声明:
class Test{
a: string = '';
b: string = '';
c: string = '';
...
}
[顺便提一句。看起来很糟糕]只写类型
class Test{
a: type_with_default_value;
b: type_with_default_value;
c: type_with_default_value;
}
漂亮多了
解决方案
您可以使用默认值定义一个常量,并让推理处理类型
const noString = '' // you can specify the type of the const
class Test {
a = noString;
b = noString;
c = noString;
}
在 Typescript 中,类型和值共享不同的宇宙。类型注释无法同时为字段分配默认值。
推荐阅读
- php - 如何保存多个cookie?
- firebase - 除云存储规则外如何匹配?
- python - 循环通过python默认字典
- c# - 无法完全避免表格闪烁
- javascript - 如何将 facebook messenger 聊天机器人连接到 mongodb 数据库以通过与 messenger bot 交互来发布请求并获取响应
- sql - Bigquery:获取表名作为列值
- fullcalendar - 当开始时间在晚上 11:00 左右时,事件在完整日历的日视图和周视图中拆分
- python - 使用 Python 将 HTML 标记添加到文本中
- javascript - 如何使用 forEach 列出部分总和
- matrix - 从无向图中提取集群/节点集