typescript - 能够从不相关的类型生成 TypeScript 元组?
问题描述
我发现了这个有趣的元组技巧,它基本上做了以下事情:
export type Lit = string | number | boolean | undefined | null | void | {};
export const tuple = <T extends Lit[]>(...args: T) => args;
const arr = tuple(1, 2, 3, 'hello', new Date, false)
// typeof arr is [1, 2, 3, 'hello', Date, false]
(游乐场链接)
但是,如果我更改 的定义Lit
,它仍然有效:
export type Lit = symbol | {};
export const tuple = <T extends Lit[]>(...args: T) => args;
const arr = tuple(1, 2, 3, 'hello', new Date, false)
// typeof arr is [1, 2, 3, 'hello', Date, false]
(游乐场链接)
或者如果我设置Lit
为
export type Lit = '' | {};
它仍然有效!(游乐场链接)
但是,如果我将 Lit 更改为其他内容,例如
export type Lit = undefined | {};
然后我得到一个不同的结果类型arr
:
const arr = tuple(1, 2, 3, 'hello', new Date, false)
// typeof arr is [number, number, number, string, Date, boolean]
(游乐场链接)
这里发生了什么?
解决方案
推荐阅读
- python - Matplotlib 条形图中条形上方的值
- c# - 使用枚举进行位移
- docker - Hyperledger Fabric Raft Orderer 设置失败,通道创建时身份验证握手失败
- java - Spring Boot JPA 多对多关联与导致多包异常的额外列
- c# - 将两个不同的列表导出为一个字符串
- c++ - 自动“是”到 Popen SSH 命令
- maven - 如何使这个jsp页面在带有eclipse的Tomcat上工作?
- ios - SwiftUI 中的 .transition 修饰符
- docker - 从存储库创建 DockerFile
- ruby-on-rails - 有没有办法省略 Active Record 转换(来自 postgres 范围)?