---
title: "TypeScript"
description: "Extract variant types with VariantProps and require specific variants with TypeScript utility types."
url: "https://cva.style/getting-started/typescript/"
---

## Extracting variant types

`cva` offers the `VariantProps` helper to extract variant types

components/button.ts

```ts
import type { VariantProps } from "class-variance-authority";
import { cva, cx } from "class-variance-authority";


/**
 * Button
 */
export type ButtonProps = VariantProps<typeof button>;
export const button = cva(/* … */);
```

## Required variants

To keep the API small and unopinionated, `cva` **doesn’t** offer a built-in solution for setting required variants.

Instead, we recommend using TypeScript’s [Utility Types](https://www.typescriptlang.org/docs/handbook/utility-types.html):

components/button.ts

```ts
import { cva, type VariantProps } from "class-variance-authority";


export type ButtonVariantProps = VariantProps<typeof buttonVariants>;
export const buttonVariants = cva("…", {
  variants: {
    optional: { a: "…", b: "…" },
    required: { a: "…", b: "…" },
  },
});


/**
 * Button
 */
export interface ButtonProps
  extends
    Omit<ButtonVariantProps, "required">,
    Required<Pick<ButtonVariantProps, "required">> {}


export const button = (props: ButtonProps) => buttonVariants(props);


// ❌ TypeScript Error:
// Argument of type "{}": is not assignable to parameter of type "ButtonProps".
//   Property "required" is missing in type "{}" but required in type
//   "ButtonProps".
button({});


// ✅
button({ required: "a" });
```
