Typescript类型体操 - BEM style string
题目
中文
Block,Element,Modifier 方法(BEM)是 CSS 中类的流行命名约定。
例如,块组件用 btn 表示,依赖于块的元素用 btn__price
表示,更改块样式的修饰符将用 btn--big
或者 btn__price--warning
表示。
实现从这三个参数生成字符串联合的 BEM<B, E, M>
。其中 B
是字符串字面量,E
和 M
是字符串数组(可以为空)。
English
The Block, Element, Modifier methodology (BEM) is a popular naming convention for classes in CSS.
For example, the block component would be represented as btn
, element that depends upon the block would be represented as btn__price
, modifier that changes the style of the block would be represented as btn--big
or btn__price--warning
.
Implement BEM<B, E, M>
which generate string union from these three parameters. Where B
is a string literal, E
and M
are string arrays (can be empty).
答案
type BEM<
B extends string,
E extends string[],
M extends string[]
> = `${B}${E extends [] ? '' : `__${E[number]}`}${M extends []
? ''
: `--${M[number]}`}`;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具