You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NextJS/ReactJS传递variant属性给shadCn Button时出现TypeScript错误

解决shadcn Button组件TypeScript variant属性错误及Next.js组件类似问题

问题描述

在Next.js/React项目中使用shadcn的Button组件时,传递variant属性触发TypeScript错误:

Type '{ children: string; variant: string; }' is not assignable to type 'IntrinsicAttributes & ButtonProps & RefAttributes'.
Property 'variant' does not exist on type 'IntrinsicAttributes & ButtonProps & RefAttributes'.ts(2322)

业务代码示例

export default function Home() {
  return (
   <main>
     <Button variant="outline">Button</Button>
   </main>
  )
}

shadcn Button组件代码

import * as React from "react"
import { Slot } from "@radix-ui/react-slot"
import { cva, type VariantProps } from "class-variance-authority"

import { cn } from "@/lib/utils"

const buttonVariants = cva(
  "inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
  {
    variants: {
      variant: {
        default: "bg-primary text-primary-foreground hover:bg-primary/90",
        destructive:
          "bg-destructive text-destructive-foreground hover:bg-destructive/90",
        outline:
          "border border-input bg-background hover:bg-accent hover:text-accent-foreground",
        secondary:
          "bg-secondary text-secondary-foreground hover:bg-secondary/80",
        ghost: "hover:bg-accent hover:text-accent-foreground",
        link: "text-primary underline-offset-4 hover:underline",
      },
      size: {
        default: "h-10 px-4 py-2",
        sm: "h-9 rounded-md px-3",
        lg: "h-11 rounded-md px-8",
        icon: "h-10 w-10",
      },
    },
    defaultVariants: {
      variant: "default",
      size: "default",
    },
  }
)

export interface ButtonProps
  extends React.ButtonHTMLAttributes<HTMLButtonElement>,
    VariantProps<typeof buttonVariants> {
  asChild?: boolean
}

const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
  ({ className, variant, size, asChild = false, ...props }, ref) => {
    const Comp = asChild ? Slot : "button"
    return (
      <Comp
        className={cn(buttonVariants({ variant, size, className }))}
        ref={ref}
        {...props}
      />
    )
  }
)
Button.displayName = "Button"

export { Button, buttonVariants }

同时,项目内其他Next.js组件(如Next/image)也出现类似类型警告。

解决方案

1. 统一文件后缀为.tsx

确保组件定义文件和使用组件的页面/组件文件都使用.tsx后缀,TypeScript无法在.js/.jsx文件中正确解析TS类型定义。

2. 修正TypeScript配置

检查项目根目录的tsconfig.json,确保关键配置正确:

{
  "compilerOptions": {
    "target": "ESNext",
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "jsx": "preserve",
    "module": "ESNext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "allowJs": true,
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "isolatedModules": true,
    "noEmit": true
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}
  • 确保strict设为true,启用严格类型检查
  • include必须覆盖所有TS/TSX文件

3. 验证类型导入正确性

确认VariantProps是类型导入:

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

若省略type关键字,会导致类型被当作值导入,引发识别异常。

4. 重启TypeScript服务

在VS Code中按下Ctrl+Shift+P(Windows)/Cmd+Shift+P(Mac),选择TypeScript: Restart TS Server,清除类型缓存。

5. 更新依赖版本

确保相关依赖版本兼容,执行命令更新:

npm install @types/react@latest @types/react-dom@latest class-variance-authority@latest

内容的提问来源于stack exchange,提问作者Rabindra Mahato

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 03:29:57