fix: make preset name type infered

This commit is contained in:
p-sw 2024-05-20 16:17:21 +09:00
parent 73114abd0e
commit ea3ed882b0

View File

@ -76,12 +76,11 @@ type VariantKV<V extends VariantType> = {
* } * }
* ``` * ```
*/ */
type PresetType<V extends VariantType> = Record< type PresetType<V extends VariantType, N extends string> = {
string, [PresetName in N]: Partial<VariantKV<V>> & { className?: string };
Partial<VariantKV<V>> & { className?: string } };
>;
export function vcn<V extends VariantType>({ export function vcn<V extends VariantType, N extends string>({
base, base,
variants, variants,
defaults, defaults,
@ -90,12 +89,12 @@ export function vcn<V extends VariantType>({
base?: string | undefined; base?: string | undefined;
variants: V /* VariantType */; variants: V /* VariantType */;
defaults: VariantKV<V>; defaults: VariantKV<V>;
presets?: PresetType<V>; presets?: PresetType<V, N>;
}): [ }): [
( (
variantProps: Partial<VariantKV<V>> & { variantProps: Partial<VariantKV<V>> & {
className?: string; className?: string;
preset?: keyof PresetType<V>; preset?: N | undefined;
} }
) => string, ) => string,
<AnyPropBeforeResolve extends Record<string, any>>( <AnyPropBeforeResolve extends Record<string, any>>(
@ -107,7 +106,7 @@ export function vcn<V extends VariantType>({
) => [ ) => [
Partial<VariantKV<V>> & { Partial<VariantKV<V>> & {
className?: string; className?: string;
preset?: keyof PresetType<V>; preset?: N | undefined;
}, },
Omit< Omit<
AnyPropBeforeResolve, AnyPropBeforeResolve,
@ -124,7 +123,7 @@ export function vcn<V extends VariantType>({
* @returns The class name. * @returns The class name.
*/ */
({ className, preset, ...variantProps }) => { ({ className, preset, ...variantProps }) => {
const currentPreset: PresetType<V>[string] | null = const currentPreset: PresetType<V, N>[N] | null =
presets && preset ? presets[preset] ?? null : null; presets && preset ? presets[preset] ?? null : null;
const presetVariantKeys: (keyof V)[] = Object.keys(currentPreset ?? {}); const presetVariantKeys: (keyof V)[] = Object.keys(currentPreset ?? {});
return twMerge( return twMerge(
@ -191,7 +190,7 @@ export function vcn<V extends VariantType>({
* } * }
* ``` * ```
*/ */
export type VariantProps<F extends ReturnType<typeof vcn>[0]> = F extends ( export type VariantProps<F extends (props: any) => string> = F extends (
props: infer P props: infer P
) => string ) => string
? P ? P