typescript - 使用其他类型作为变量创建复合类型
问题描述
让我们假设我有以下类型(只是一个例子来说明我的观点):
type Alignment =
"top left" |
"middle left" |
"bottom left" |
"top center" |
"middle center" |
"bottom center" |
"top right" |
"middle right" |
"bottom right";
有可能做这样的事情吗?
type Vertical = "top" | "middle" | "bottom";
type Horizontal = "left" | "center" | "right";
type Alignment = `${Vertical} ${Horizontal}`;
编辑所以我看到我的问题已经过时了,根本不是关于打字稿的,抱歉,但感谢您的回答,真的很有帮助。
无论如何,我在 VSCode 中遇到了这个错误,知道吗?
解决方案
是的,从 Typescript 4.1 版开始就有可能!
您可以在此处阅读有关模板文字类型的信息 - https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html
Here's your code in the Typescript playground where you can hover on the Alignment
type to see what it looks like:
https://www.typescriptlang.org/play?#code/C4TwDgpgBAahBOwCWBjAhgGygXigImAHsw8oAffAWyQBMaMJSK8AjQ4IyvAbgChRIUABKF4SAF6EAdsEw58DAGbAm+FBBkJVeMQHMAFir4DoAQQxJdUyhuDyABgBIA3nESpMAXyguRYyTJe9txAA
推荐阅读
- java - 使用 google flogger 记录 ClassNotFoundException: com.google.common.flogger.backend.system.DefaultPlatform 时未找到记录平台
- java - 如何在没有 .0 的情况下打印双精度值
- vba - 使用组合框过滤拆分表单数据表
- c - "预期 ')' 匹配这个 '(' "添加 & 到函数原型时
- laravel - 从 Laravel 中的 ID 获取正确的值
- matlab - BPSK 角度偏差
- c++ - 如何修复以下 C++ 代码中的 C1202 错误?
- sql - 基于 SQL CASE WHEN 或 IIF 反转布尔值
- powershell - 如何通过 Powershell / Selenium-Chrome 驱动程序对防火墙网页进行身份验证?
- r - 问题:在 R 中,线框错误;里面什么都没有的空盒子