简体   繁体   English

'{ rating: any; 类型中缺少属性 'onClick' }' 但在“RatingType”类型中是必需的

[英]Property 'onClick' is missing in type '{ rating: any; }' but required in type 'RatingType'

I have a simple rating component in reactjs with typescript as follows我在 reactjs 和 typescript 中有一个简单的评级组件,如下所示

Rating.js Rating.js

export type RatingType = {
  rating: number,
  onClick: (x: number) => void
  style?: React.CSSProperties,
  prod?: any
}

const Rating: React.FC <RatingType> = ({ rating, onClick, style }) => {
  return (
    <>
      //do something here
    </>
  );
};

export default Rating;

I would like to use this rating component in another component but without the onClick function like this below我想在另一个组件中使用此评级组件,但没有 onClick function 如下所示

import Rating from "./Rating";
function Demo() {
  return (
    //logic goes here
    <Rating rating={item.ratings}  />
  )
}

export default Demo

But I get the following error但我收到以下错误

Property 'onClick' is missing in type '{ rating: any; }' but required in type 'RatingType'.

How can I make the onClick function option when defining the type RatingType?在定义类型 RatingType 时,如何制作 onClick function 选项?

If you don't want onClick to be required, just make it optional like this:如果您不希望onClick是必需的,只需将其设为可选,如下所示:

export type RatingType = {
  rating: number,
  onClick?: (x: number) => void // <- Add question mark there
  style?: React.CSSProperties,
  prod?: any
}

const Rating: React.FC <RatingType> = ({ rating, onClick, style }) => {
  return (
    <>
      //do something here
    </>
  );
};

export default Rating;

To make onClick optional just change RatingType to:要使onClick可选,只需将RatingType更改为:

export type RatingType = {
  rating: number,
  onClick?: (x: number) => void, // NOTE the ? to make property optional
  style?: React.CSSProperties,
  prod?: any
}

Note the ?注意? after the onClick onClick

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “ReactElement”类型中缺少属性“...”<any, any> ' 但在类型 '{...}' 中是必需的</any,> - Property '...' is missing in type 'ReactElement<any, any>' but required in type '{...}' 类型中缺少属性,但类型中需要属性 - Property is missing in type but required in type 类型“ any []”中缺少属性“ 0”,但是类型“ [{{id:string; gp:布尔值; }] - Property '0' is missing in type 'any[]' but required in type '[{ id: string; gp: boolean; }] 类型“{}”中缺少属性“submitAction”,但在类型中是必需的 - Property 'submitAction' is missing in type '{}' but required in type 类型“ any []”的参数不能分配给类型“ A”的参数。 类型“ any []”中缺少属性“ a” - Argument of type 'any[]' is not assignable to parameter of type 'A'. Property 'a' is missing in type 'any[]' “文件[]”类型中缺少属性“项目”,但在“文件列表”类型中是必需的 - Property 'item' is missing in type 'File[]' but required in type 'FileList' 类型'import(“cesium”).BoundingSphere'中缺少属性'intersect',但在类型中是必需的 - Property 'intersect' is missing in type 'import(“cesium”).BoundingSphere' but required in type 类型“{}”中缺少属性“profileStore”,但类型“只读”中需要<AppProps> &#39;.ts(2741) - Property 'profileStore' is missing in type '{}' but required in type 'Readonly<AppProps>'.ts(2741) Typescript 错误:类型中缺少属性“children”,但类型“CommonProps”中是必需的 - Typescript error: Property 'children' is missing in type but required in type 'CommonProps' 类型中缺少 TypeScript 属性“导航”,但类型“道具”React Native 中需要 - TypeScript Property 'navigation' is missing in type but required in type 'Props' React Native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM