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

VSCode无法悬停显示复杂接口React组件的缺失属性

React Button组件TypeScript类型提示缺失详细信息问题

我已经大量搜索并查阅相关问题,但未找到解决方案。问题如下:

我定义了一个名为Button的React组件,其属性类型为:

type ButtonProps = RequireAtLeastOne<BaseButtonProps, "children" | "label" | "icon" | "CustomIcon">;

其中RequireAtLeastOne用于约束必须传入指定属性中的至少一个,BaseButtonProps的定义为:

interface BaseButtonProps
  extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, "style"> {
  CustomIcon?: React.FunctionComponent<React.SVGAttributes<SVGElement>>;
  variant?: btnColorVariant;
  label?: string;
  size?: "lg" | "default" | "sm" | "xs";
  icon?:
    | "add"
    | "edit"
    | "delete"
    | "close"
    | "camera"
    | "back"
    | "checkList"
    | "upload"
    | "document"
    | "archive";
  disabled?: boolean;
  loading?: boolean;
  sx?: CSSObject;
  children?: React.ReactNode;
  href?: string;
  active?: boolean;
}

当使用<Button />组件时,TypeScript会用红色下划线标记错误(这是正常的),但没有提供缺失必填属性的详细提示。

举个对比:其他组件未传入必填属性时,悬停会显示明确的错误信息,比如:

Type '{}' is missing the following properties from type 'CardProps': title, summaryts(2739)

但我的Button组件悬停仅显示模糊的提示:

Type '{}' is not assignable to type 'IntrinsicAttributes & ButtonProps'.ts(2322)

另外,按下Ctrl + Space虽然能调出属性列表,但所有属性都带有?标记为可选,无法区分哪些是必须至少传入一个的属性,帮助不大。

我希望TypeScript能直接显示需要至少传入的属性列表,不用每次都去查看组件类型定义。


内容的提问来源于stack exchange,提问作者Tayyab Ferozi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 07:02:13