BEM Style String
介绍
BEM是CSS中类的流行的命名规范.
例如, 用btn
表示块元素, 依赖于块元素的将用btn__price
表示, 改变块样式的修饰符适用btn--big
或btn__price--warning
来修饰.
执行BEM<B,E,M>
,从3个参数中生成联合字符串. B是字符字面量, E和M是字符数组(也可能是空的).
ts
View on GitHubts
起点
ts
/* _____________ Your Code Here _____________ */typeBEM <B extends string,E extends string[],M extends string[]> = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <BEM <'btn', ['price'], []>, 'btn__price'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <BEM <'btn', ['price'], ['warning', 'success']>, 'btn__price--warning' | 'btn__price--success' >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <BEM <'btn', [], ['small', 'medium', 'large']>, 'btn--small' | 'btn--medium' | 'btn--large' >>,]
take the challengets
/* _____________ Your Code Here _____________ */typeBEM <B extends string,E extends string[],M extends string[]> = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <BEM <'btn', ['price'], []>, 'btn__price'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <BEM <'btn', ['price'], ['warning', 'success']>, 'btn__price--warning' | 'btn__price--success' >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <BEM <'btn', [], ['small', 'medium', 'large']>, 'btn--small' | 'btn--medium' | 'btn--large' >>,]
解决方案
Spoiler warning // Click to reveal answer
ts
// most populartypeBEM <B extends string,E extends string[],M extends string[]> = `${B }${E extends [] ? '' : `__${E [number]}`}${M extends [] ? '' : `--${M [number]}`}`
ts
// most populartypeBEM <B extends string,E extends string[],M extends string[]> = `${B }${E extends [] ? '' : `__${E [number]}`}${M extends [] ? '' : `--${M [number]}`}`
ts
// my solution/*** 如果E类型数组中有多个元素,就不能处理.*/typeaddModifierLine <Pre extends string,A extends any[]> =A ['length'] extends 0?A :A extends [inferF extends string, ...inferR ]? `${Pre }${F }` | (R ['length'] extends 0 ? never :addModifierLine <Pre ,R >):A ;typeaddModifierLineToArrEle <Pre extends string,A extends any[]> =A extends [inferF extends string, ...inferR ]? [`${Pre }${F }`, ...addModifierLineToArrEle <Pre ,R >]: [];typemergeEAndM2 <E extends string[],M extends string[]> =M ['length'] extends 0?E :E extends [inferF extends string]?addModifierLineToArrEle <F ,M >:M ;typeBEM <B extends string,E extends string[],M extends string[]> =addModifierLine <B ,mergeEAndM2 <addModifierLineToArrEle <'__',E >,addModifierLineToArrEle <'--',M >>>
ts
// my solution/*** 如果E类型数组中有多个元素,就不能处理.*/typeaddModifierLine <Pre extends string,A extends any[]> =A ['length'] extends 0?A :A extends [inferF extends string, ...inferR ]? `${Pre }${F }` | (R ['length'] extends 0 ? never :addModifierLine <Pre ,R >):A ;typeaddModifierLineToArrEle <Pre extends string,A extends any[]> =A extends [inferF extends string, ...inferR ]? [`${Pre }${F }`, ...addModifierLineToArrEle <Pre ,R >]: [];typemergeEAndM2 <E extends string[],M extends string[]> =M ['length'] extends 0?E :E extends [inferF extends string]?addModifierLineToArrEle <F ,M >:M ;typeBEM <B extends string,E extends string[],M extends string[]> =addModifierLine <B ,mergeEAndM2 <addModifierLineToArrEle <'__',E >,addModifierLineToArrEle <'--',M >>>