vanilla-extract-css / vanilla-extract

Zero-runtime Stylesheets-in-TypeScript

Home Page:https://vanilla-extract.style

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Recipe's runtime functions do not accept `undefined` as valid values for variants at the type level (`exactOptionalPropertyTypes`)

DominikRusso opened this issue · comments

Describe the bug

With TypeScript's exactOptionalPropertyTypes enabled, recipe's runtime functions do not accept undefined as valid values for variants at the type level.

Reproduction

  • Add "exactOptionalPropertyTypes": true to compilerOptions in tsconfig.json.
  • Copy the code below into a typescript file.
  • Observe the error in your editor or run pnpm lint:tsc.
import { recipe } from "@vanilla-extract/recipes";

const r = recipe({
	variants: {
		color: {
			neutral: { background: "whitesmoke" },
		},
	},
});

const color: "neutral" | undefined = undefined as "neutral" | undefined;

r({ color });

Without exactOptionalPropertyTypes r's type is
(options?: { color?: "neutral" | undefined; } | undefined) => string.
With it enabled the type changes to
(options?: { color?: "neutral"; } | undefined) => string,
therefore the example above doesn't satisfy the type checker.

tests/recipes/test.ts:13:3 - error TS2379: Argument of type '{ color: "neutral" | undefined; }' is not assignable to parameter of type '{ color?: "neutral"; }' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
  Types of property 'color' are incompatible.
    Type '"neutral" | undefined' is not assignable to type '"neutral"'.
      Type 'undefined' is not assignable to type '"neutral"'.

r({ color });

System Info

I'm using typescript 5.5.3, though the problem should affect all typescript versions since 4.4.0.

Validations

Fixed in #1443