javascript - 在 Storybook 道具中扩展 react-select v5 会导致未知类型
问题描述
我正在寻找扩展打字稿定义,react-select
以便我可以将...args
Storybook 中的自定义传递给组件。
选择数据
export interface FishOption {
readonly value: string;
readonly label: string;
readonly isFixed?: boolean;
readonly isDisabled?: boolean;
}
export const fishOptions: readonly FishOption[] = [
{ value: 'Maguro', label: 'Maguro' },
{ value: 'Unagi', label: 'Unagi' },
{ value: 'Shishamo', label: 'Shishamo' },
{ value: 'Toro', label: 'Toro' },
{ value: 'Iwashi', label: 'Iwashi' }
];
node_modules/react-select/dist/declarations/src/index.d.ts
export type { StateManagerProps as Props } from './useStateManager';
选择.stories.tsx
import { Story, Meta } from '@storybook/react';
import { useState } from 'react';
import Select, { Props } from 'react-select';
import { FishOption, fishOptions } from './Select.data';
export default {
component: Select,
title: 'Components/Select',
argTypes: { onChange: { action: '' } }
} as Meta;
// Extended interface.
interface ExtendedProps extends Props {
someProp: boolean;
}
// Use extended interface.
const Template: Story<ExtendedProps> = args => {
const [value, setValue] = useState<FishOption>();
const handleChange = (newValue: FishOption) => {
setValue(newValue);
};
return <Select defaultValue={value} onChange={handleChange} {...args} />;
};
export const Single = Template.bind({});
Single.args = {
someProp: true,
options: fishOptions
};
我的onChange
处理程序出现以下打字稿错误:
Type '(newValue: FishOption) => void' is not assignable to type '(newValue: unknown, actionMeta: ActionMeta<unknown>) => void'.
Types of parameters 'newValue' and 'newValue' are incompatible.
Type 'unknown' is not assignable to type 'FishOption'.ts(2322)
查看其他属性,其他 prop (defaultValue) 类型也设置unknown
为。我不确定我是错误地导入类型还是某种擦除?正在发生。
如何扩展react-select
v5 道具以便我可以在 Storybook 或其他地方使用它们?
相关链接:在 typescript 中扩展 react-select 接口属性
CodeSandbox:https ://codesandbox.io/s/interesting-hypatia-2vv6p (没有故事书,但观察 ExtendedSelect 以查看道具unknown
)
解决方案
推荐阅读
- javascript - 从 json 文件中检测代码中使用了哪些属性
- typescript - 使用 Type Guards 在 typescript 上运行 jest 时出错
- google-calendar-api - Google 服务帐户:委托域范围的授权以仅模拟一个用户
- tcl - 我们可以用 Tcl_FSOpenFileChannel 打开 gz 文件吗
- javascript - 我们可以在html/js/php中像视频编辑器一样混合音视频吗
- node.js - 从 API 中的请求中获取 FormData
- python - 在 DataFrame 中按结果过滤组
- .net - EF6 DbContext 在处置时是否回滚打开的事务?
- tensorflow - 在连接深度特征向量和手工制作的特征向量后,我可以使用最终池化层来找到最佳共同特征吗?
- c++ - 引用其他文件中的对象导致“...”未被捕获(节点 MCU)