我有一个组件,它接受一个已知具有 ID 的项目列表,以及一个过滤这些项目的函数。
带有 ID 的类型是项目的通用类型,所有项目都会有。
但更具体的项目将包括其他 Prop 。
type GenericItem = {
id: string;
}
type SpecificItem = {
id: string;
someOtherProp: boolean;
}
我还有一个函数类型,它使用泛型类型进行操作。
type GenericItemFunction = (item: GenericItem) => boolean;
然后我有这个组件,它在其 props 中使用了 GenericItem 和 GenericItemFunction。
type CompProps = {
fn: GenericItemFunction;
items: GenericItem[];
}
const Comp: React.FC<CompProps> = ({ fn, items }) => <></>;
当我尝试将此组件与特定类型一起使用时,出现错误提示我无法使用 GenericItemFunction 的实现,因为项目的类型不兼容。
const App = () => {
const items: SpecificItem[] = [];
const filter = (item: SpecificItem) => item.someOtherProp;
return (
<Comp
fn={filter} // error on `fn` prop
items={items}
/>
)
}
我收到的 typescript 错误是:
Type '(item: SpecificItem) => boolean' is not assignable to type 'GenericItemFunction'.
Types of parameters 'item' and 'item' are incompatible.
Property 'someOtherProp' is missing in type 'GenericItem' but required in type 'SpecificItem'.
我想我有两个问题;
首先,当两种类型都需要 id: string
属性时,为什么会发生冲突?
其次,有没有更理智的方法来做这样的事情?
我的第一个想法是 GenericItemFunction
上的 item
的类型可以从提供给 App 组件中的 items
属性的值中推断出来.
但老实说,我不确定那会是什么样子......
我的另一个想法是让 Comp
成为通用的,但不显示使用使用泛型的 React 组件......似乎 jsx/tsx 并不真正支持该语法。
我希望像这样的东西会引发各种错误。
const Comp = <T extends GenericItem,>({ fn, items }) => <></>;
const App = () => {
return <Comp<SpecificType> />;
}
最后,我确实尝试了这个并且没有任何错误。但缺点是 items
的类型被推断为任何类型。
type GenericItem = {
id: string;
}
type SpecificItem = {
id: string;
someOtherProp: boolean;
}
type GenericItemFunction <T> = (item: T) => boolean;
type CompProps <T extends GenericItem = any> = {
fn: GenericItemFunction<T>;
items: T[];
}
const Comp: React.FC<CompProps> = ({ fn, items }) => <></>;
const App = () => {
const items: SpecificItem[] = [];
const filter = (item: SpecificItem) => item.someOtherProp;
return (
<Comp
fn={filter}
items={items}
/>
)
}
这是我一直在使用的 playground 的链接。
https://www.typescriptlang.org/play?#code/C4TwDgpgBA4hB2EBOBLAxgSWBAtlAvFAN4BQAkCgCYBcUAzsKvAOYDcJAviSaJFAMqQ0KAGbosuAsRJRZUKrQZM2MuXQD2OCAHlgAC2QAFJOrC0ARuvUAbCAEN47Lj3DQ4iVJmw4AYgFd4NGAUdXgoAB4AFQA+KQAKFG9aSIBKAljLG3tHbhc+AGFNMGNTOgjIqAgAD2x4SjL3ZHFvKQcQWMJSMhF4WkbPCV8AoJD4KOj2OXlvOmSAbQBdJxI0UIYoQpwzKAAleyCAOh988M3ikzA6Dqg4oigegBpp3DKONPxY8OjwgHoJ7lW8HWAEEwGB4u9YqQpoD1okXrRBBBhGIvLhFlJFpM5LDgPcUNZsEh4vCcIihKJmrhIc8cAcNFpdAYkCUwOxVLIkBBgH4kGE4hyphEzoKhXIevgiGJCcguGKxaS6JLFXL5X9BSlOEA
更新:
Why did you use any as default for GenericItem type? Without this I believe it should properly infer Genericitem from GenericItemFunction. – tymzap
删除 CompProps
typedef 的 = any
会导致 Comp
声明中的错误...
type CompProps <T extends GenericItem> = {
fn: GenericItemFunction<T>;
items: T[];
}
const Comp: React.FC<CompProps> = ({ fn, items }) => <></>; // this line has the error
Generic type 'CompProps' requires 1 type argument(s).
意思是,我仍然需要在某处声明类型。这意味着在使用组件之前我需要知道 GenericItem
类型的变体。
SpecificItem
只是碰巧与 GenericItem
typedef 重叠的类型的表示。
在大多数情况下,Comp
不知道实际将使用什么类型,并且 any 不会向作者提供任何有用的信息。
我希望有类似...
type CompProps <T extends GenericItem> = {
items: <T extends GenericItem>[];
fn: <infer from T>[];
}
但我不确定这是否存在,或者类似的东西。
最佳答案
:脸_手掌:
CompProps
使用 <T extends GenericType = any>
是执行此操作的正确方法。
type CompProps <T extends GenericItem = any> = {
items: T[];
filter: (item: T) => boolean;
}
const Comp: React.FC<CompProps> = (props) => <></>;
魔法在这里发生:
const App = () => {
...
const filter = (item: SpecificItem) => item.someOtherProp;
...
}
const Comp: React.FC<CompProps>
不关心通用类型是什么,因此 = any
.只要它的形状至少与 GenericType
相同.
但是在App
里面组件,我们在其中定义过滤器属性的方法,我们声明 SpecificItem
参数中的 typedef。只有 that 方法使用 SpecificItem
类型定义。 CompProps
只关心它是否满足 GenericItem
所需的形状.
希望对某人有所帮助。
https://stackoverflow.com/questions/71836109/
相关文章:
javascript - 在 Promise.all 中 react setState
java - 使用 'fib(N) = [Phi^N – phi^N]/Sqrt[5]' 公式计算
javascript - OpenSea 错误 - 请使用 providerUtils.standa
node.js - 在 Github 操作中获取错误 : connect ECONNREFUSED
python - 使用 Pandas Align 时,时间序列数据帧返回错误 - valueErro
xamarin - MAUI Shell 从侧面移除 OverScroll