惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyberwarzone
Cyberwarzone
V
Vulnerabilities – Threatpost
T
Tenable Blog
Forbes - Security
Forbes - Security
Simon Willison's Weblog
Simon Willison's Weblog
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
S
Securelist
C
Cybersecurity and Infrastructure Security Agency CISA
Project Zero
Project Zero
C
CXSECURITY Database RSS Feed - CXSecurity.com
V
Visual Studio Blog
WordPress大学
WordPress大学
Latest news
Latest news
K
Kaspersky official blog
T
Tailwind CSS Blog
T
Threat Research - Cisco Blogs
B
Blog RSS Feed
C
Cisco Blogs
博客园 - 聂微东
Martin Fowler
Martin Fowler
T
The Blog of Author Tim Ferriss
小众软件
小众软件
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Privacy International News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CERT Recently Published Vulnerability Notes
Cisco Talos Blog
Cisco Talos Blog
S
SegmentFault 最新的问题
Security Latest
Security Latest
Y
Y Combinator Blog
爱范儿
爱范儿
aimingoo的专栏
aimingoo的专栏
P
Privacy & Cybersecurity Law Blog
L
LINUX DO - 最新话题
月光博客
月光博客
The GitHub Blog
The GitHub Blog
博客园 - 三生石上(FineUI控件)
S
Security Affairs
P
Proofpoint News Feed
D
DataBreaches.Net
有赞技术团队
有赞技术团队
云风的 BLOG
云风的 BLOG

Ray's Blog

Ray's Blog Ray's Blog Ray's Blog Ray's Blog Ray's Blog Ray's Blog Ray's Blog Ray's Blog Ray's Blog Ray's Blog Ray's Blog Ray's Blog Ray's Blog Ray's Blog Ray's Blog Ray's Blog
Ray's Blog
i@mk1.io (Ra · 2023-05-13 · via Ray's Blog

TypeScript 小寄巧!如何在不使用 const 泛型修饰符的情况下推导出列表字面量?

在开始之前我们先来看几个代码片段:

const a = <T extends string>(t: T) => t;
const b = <T extends number>(t: T) => t;
const c = <T extends boolean>(t: T) => t;

此时我们这样调用它们,请问返回值的类型是什么?

const d = a("a");
const e = b(1);
const f = c(true);

好了不卖关子了,上面三个变量的类型分别是"a"1true。很符合直觉对吧?他们的参数都是字面量类型,因此泛型 T 也是对应的字面量,返回值 T 便是传入参数本身。

我们现在得到了这些能够从参数推导出字面量的函数,让我们把它推广到列表试试(Playground 链接):

const g = <T extends string[]>(t: T) => t;

const h = g(["111", "222"]); // 寄,类型是string[]

奇怪,为什么类型是 string[] 而不是更为具体的 ["111", "222"] 呢?或许是因为这个列表能够被函数体所修改罢,谁知道呢。

如果我们确实想让返回值的类型和传入参数的类型所匹配,但不想加上 as const 修饰符(因为它会让类型变为 readonly ["111","222"]),那我们怎么做呢?最近 TypeScript 5.0 的更新中加入了 const 泛型修饰符,能够在不用 as const 断言的情况下推导出字面量类型,然而它的结果也是 readonly ,这不是我们所想要的

其实你只需要做一些小小的改动(Playground 链接):

const g = <T extends string[]>(t: [...T]) => t; // 这里t的类型用了一个展开运算

const h = g(["111", "222"]); // 好,类型变成["111", "222"]了

就可以得到我们想要的结果。

不清楚为什么能这么用,不过用就完事了