[React Typescript] Ensure correct inference for prop types with satisfies & ComponentProps

发布时间 2023-08-07 15:08:42作者: Zhentiw
import { ComponentProps } from "react";
import { Equal, Expect } from "../helpers/type-utils";

const buttonProps = {
  type: "button",
  // @ts-expect-error
  illegalProperty: "I AM ILLEGAL",
} as const satisfies ComponentProps<'button'>;

<>
  <button {...buttonProps}>Click Me!</button>
</>;

const buttonPropType = buttonProps.type;

type test = Expect<Equal<typeof buttonPropType, "button">>;

In this example, you can omit as const, it still works