VSCode无法悬停显示复杂接口React组件的缺失属性
我已经大量搜索并查阅相关问题,但未找到解决方案。问题如下:
我定义了一个名为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
相关产品推荐
相关产品推荐

