Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
export type ClassName = string | boolean | null | undefined;

type FilterStrings<T extends readonly unknown[]> = T extends readonly [
infer F,
...infer R,
]
? F extends string
? [F, ...FilterStrings<R>]
: FilterStrings<R>
: [];

type JoinStrings<T extends readonly string[]> = T extends readonly [
infer F,
...infer R,
]
? F extends string
? R extends readonly string[]
? R["length"] extends 0
? F
: `${F} ${JoinStrings<R>}`
: F
: never
: "";

/**
* Conditionally join classNames into a single string
* @param {...String} args The expressions to evaluate
* @returns {String} The joined classNames
*/
function cx(...args: ClassName[]): string;
function cx<T extends readonly ClassName[]>(
...args: T
): JoinStrings<FilterStrings<T>>;
function cx(): string {
let str = "",
i = 0,
Expand Down
Loading