string - 如何覆盖 TypeScript 类型别名的 VSCode 工具提示?
问题描述
以下代码不起作用:
type char = 'a' | 'b' | 'c' | 'd' | 'e' | 'f';
const s: string = 'foo';
const [c]: char = s;
// [ERROR]: Type 'string' is not assignable to type 'char'.
我可以这样做:
type char = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | string;
但这会导致两个问题:
解决方案
如何覆盖 TypeScript 类型别名的 VSCode 工具提示?
当您有一个可以解析为更简单的联合时,TypeScript 将始终显示更简单的版本
例子:
type char = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | string;
// resolves to the following simpler type so that is what TypeScript will show.
type char = string;
推荐阅读
- amazon-web-services - AWS EC2 快照还原
- angular - ng 测试不工作 业力无法启动“ChromeHeadless 在 2000 毫秒内没有被杀死,发送 SIGKILL。”
- django - 如何在管理界面中显示模型/数据库的帮助文本?
- json - 使用获取请求 Flask 时 Json 无效
- bash - 填充音量选择菜单 | 重击
- angular - Antd Angular在formArray中验证inputGroup
- python - 遍历 if 语句
- html - 在 Angular 组件中定位 innerHtml
- python - 如何使用 if-condition 以不同的迭代样式执行相同的命令?(Python,代码优化)
- css - 显示网格和网格模板 css 属性在 IE 11 浏览器中不起作用