我正在尝试使用泛型来DRY类似的自定义钩子。我有一些传入的工厂函数。但是我得到一个输入错误。我将代码简化为一个最小的示例:
import { useState } from 'react';
type AType = {
a: string;
};
type BType = {
b: string;
};
type AData = {
data: AType;
};
type BData = {
data: BType;
};
function useAOrB<T extends AData | BData>(factory: () => T['data']): T | undefined {
const [value, setValue] = useState<T | undefined>(undefined);
const newData = factory();
const newValue = { data: newData };
setValue(newValue);
return value;
}
const aFactory = (): AType => ({ a: 'a' });
const bFactory = (): BType => ({ b: 'b' });
export const useA = useAOrB<AData>(aFactory);
export const useB = useAOrB<BData>(bFactory);
这给了我一个类型错误:
TS2345: Argument of type '{ data: T["data"]; }' is not assignable to parameter of type 'SetStateAction<T>'.
Type '{ data: T["data"]; }' is not assignable to type '(prevState: T) => T.
Type '{ data: T["data"]; }' provides no match for the signature '(prevState: T): T'.
我不明白为什么这不能通过类型检查,所有的东西都应该匹配。我该如何解决这个问题?
直接输入新值T:
const newValue = { data: newData } as T;
//<- as T
操场上联系
相关内容
- 在typescript中,工厂函数返回的类不检查类型
- TypeScript 中的函数链工厂保留输入/输出类型
- 带有泛型的 typescript工厂模式
- TypeScript泛型工厂函数返回基于实例参数的类型而不重载
- 如何为抽象工厂模式构造 TypeScript 类型
- 没有构造函数的Typescript工厂
- 在TypeScript中用createSelector创建选择器工厂
- React/Typescript - 至少需要 '3' 个参数,但 JSX 工厂'React.createElement'最多提供 '2'。TS622
- 泛型应该是工厂方法中typescript的输出类
- TypeScript类工厂期望一个交集
- 在制作自己的useState钩子时,如何使用带有工厂的Typescript泛型作为属性?
- Sequelize TypeScript在模型工厂上调用findAll()时返回错误的项目数
- Typescript通用消息工厂
- Typescript中带有接口的通用工厂方法
- Typescript工厂中的不同接口
- 在typescript中从工厂函数返回已区分的并集
- Typescript如何导出从类工厂返回的类的类型
- TypeScript从工厂函数返回条件类型
- 使用方括号属性访问在工厂函数中定义setter会导致TypeScript错误
- 有没有一种方法可以使用键名和值类型在Typescript中编写getter工厂
最新更新
- 如何做基于名称和值的点击事件?
- 在容器和主机上创建具有相同Linux用户id的用户
- Firebase身份验证在部署后不工作.扑动Web应用程序显示空白的白色屏幕
- AWS Client VPN Access Public URL/IP
- python dict get方法即使key在dict中也运行第二个参数
- 如何在Python中使用每周时间序列
- Scss函数没有返回计算值
- 如何使用conduit获取一个lazy ByteString并将其写入文件(在恒定内存中)
- c -内存分配器-简单隔离存储:你如何从它的地址推断分配块的大小?
- 获取JSON格式的Discord PY对象
- 如何为作为prop传入的React组件设置类型
- 在c++中使用new时是否按顺序分配内存?
- 你可以暂时复制谷歌云图像文件在Python?
- PHP cURL 一个帖子但两次(双)它是相同的数据到数据库与 API
- 405 (Method Not Allowed)错误的ajax在Laravel 8
- Sicstus:一个模块和CWD
- Laravel在模型中有onethrough关系
- Whiptail:当一个仪表正在运行时,我该如何处理一个错误?
- 关于Sacha Willems的示例代码的Vulkan屏幕外渲染问题
- 有人可以分解nums[i] = nums[i-1] + nums[i]做什么?(Python) &g
- 如何在 Qwidget 上绘制二维码,我在 Windows 10 上使用 Qt Creator C++
- Fluentd td-agent.log文件大小持续增加
- 暂停javascript for循环,同时等待从突变观察者的响应?
- Jetpack撰写更新广告横幅在Android视图
- 在ASP中构建级联下拉列表的问题. NET Core 7 MVC使用JQuery和ajax.googleapis.com
- 在EAS中找不到"expo dev client"错误所依赖的"expo devel la
- 需要帮助在文件之间本地共享Javascript变量
- 在Azure中删除自定义域失败
- 如何用谷歌电子表格正确阅读的R编写CSV
- c -为什么在串行控制台的其他接口上使用UART ?
热门标签:
javascript python java c# php android html jquery c++ css ios sql mysql arrays asp.net json python-3.x ruby-on-rails .net sql-server django objective-c excel regex ruby linux ajax iphone xml vba spring asp.net-mvc database wordpress string postgresql wpf windows xcode bash git oracle list vb.net multithreading eclipse algorithm macos powershell visual-studio image forms numpy scala function api selenium