How do I remove null
as a valid variant?
#97
-
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
This comment has been minimized.
This comment has been minimized.
-
Update: currently exploring The behaviour of Changing this would be a breaking change so I’ll need to figure out the best way to approach this and batch in with some other ideas I have I’m thinking instead something like: button({ variant: “unset” }) (which would be opt-in of course) |
Beta Was this translation helpful? Give feedback.
-
Having a similar issue as I am trying to use size as an index which null breaks, instead having to override the null type definition provided by CVA. The current workaround is to use this typescript utility to wrap the VariantProps from CVA: Edit: @joe-bell The current workaround for the null type causes issues with React Prop defaults, as typescript is no longer seeing the default value applied as optional. |
Beta Was this translation helpful? Give feedback.
-
I don't want This is how I currently reduce the manual labor of doing it at once, without having to nest the types at place of usage: import {VariantProps as CvaVariantProps} from "class-variance-authority"
type Require<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>
type NonNullableProps<T> = {[P in keyof T]: NonNullable<T[P]>}
// 1. just make them all nonNullable
export type VariantProps<F extends (...args: any[]) => any> = NonNullableProps<
CvaVariantProps<F>
>
// 2. also speficy the required props
export type RequiredVariantProps<
F extends (...args: any[]) => any,
K extends keyof CvaVariantProps<F>
> = NonNullableProps<Require<CvaVariantProps<F>, K>> Not sure how efficient it is and whether TS optimizes calculations, when it has the same expression twice ( usage: // A all: non-nullable, "bar" and "foobar" required
type A = RequiredVariantProps<typeof foo, "bar" | "foobar">
// B all: non-nullable, none: required
type B = VariantProps<typeof foo> |
Beta Was this translation helpful? Give feedback.
Update: currently exploring
"unset"
keyword in[email protected]
The behaviour of
null
was defined to reflect the behaviour in Stitches.js — to disable the variant altogether — however I agree with you this is fairly confusing without that prior contextChanging this would be a breaking change so I’ll need to figure out the best way to approach this and batch in with some other ideas I have
I’m thinking instead something like:
(which would be opt-in of course)