English 中文(简体)
通用型号的配给类型
原标题:Can t assign type to generic
  • 时间:2024-02-25 02:00:02
  •  标签:
  • typescript

Can tsignonActativeChange 缩略语

type ActivityType = { 
  id: string; 
  name: string;
}

const onChange = <TOption extends {}>(value: TOption) => {
  console.log(value);
}

const onActivityChange = (value: ActivityType) => {
  console.log(value);
}

// error
const handler: typeof onChange = onActivityChange;

错误:

Type  (value: ActivityType) => void  is not assignable to type  <TOption extends {}>(value: TOption) => void .
  Types of parameters  value  and  value  are incompatible.
    Type  TOption  is not assignable to type  ActivityType .
      Type  {}  is missing the following properties from type  ActivityType : id, name

如何加以确定,并节省<代码>onChange类型背后的类型?

https://www.typescriptlang.org/play?#code/C4TwpgBAggxsAlgN0aAKABeKvKAKCQAuKAZ2ACdEA7AcwG4iTGBDAWwitoMWrQgF9OAHtGtKDIACy4UADA8mCQyoEAB7IMtXyiAfApkADYBflB17gEMQ1

http://www.ohchr.org。

使用反应剂量:

const onActivityChange = (value: ActivityType) => {
  console.log(value);
}

<Dropdown
  isError={!!errors.activity}
  id="activity"
  // error
  onChange={onActivityChange}
  data-testid="field-activity"
>
  <Option />
</Dropdown>

export type Props = {
    value?: string;
    onChange: <TOption extends {}>(value: TOption) => void;
    is错误: boolean;
    extraButtons?: ReactNode;
} & ComponentProps< div >
问题回答

通用型号应具有特定类型。

更新您的最后一行,删除错误

const handler: typeof onChange<ActivityType> = onActivityChange; 

https://www.typescriptlang.org/play?#code/C4TwpgBAggxsAlgN0aAKABeKvKAKCQAuKAZ2ACdEA7AcwG4iTGBDAWwitoMWrQgF9OAHtGKDIACy4UADA8mCQyoEAB7IMXyiAfApkADYBflB17gRRJEMQ1-GIS

There seems to be a mismatch in the types of parameters between onChange and onActivityChange. Try this

    type ActivityType = { 
  id: string; 
  name: string;
}

const onChange = <TOption extends {}>(value: TOption) => {
  console.log(value);
}

const onActivityChange = (value: ActivityType) => {
  console.log(value);
}

const handler: <TOption extends {}>(value: TOption) => void = onActivityChange;




相关问题
store data in memory with nestjs

I am trying to persist some data in my nestjs server so I can then use that data in my client app through http requests. I have created a products.service.ts file with the function getAllData() that ...

React Hook Form Error on custom Input component

I am having a problem when I use react hook form + zod in my application, in short the inputs never change value and I get the following error in the console: Warning: Function components cannot be ...

Updatable promises using proxy in JavaScript

EDIT: I ve updated this question, and the old question is moved here. A POC can be found on this jsfiddle or the snippet below GOAL: The goal here is to make or rather simulate a promise that can be ...

热门标签