[英]Typescript Error when binding can to ability instance
Simply pasting example code from docs简单地从文档中粘贴示例代码
import { createCanBoundTo } from '@casl/react';
import ability from './abilities';
export const Can = createCanBoundTo(ability);
Gives error on last line:在最后一行给出错误:
Exported variable 'Can' has or is using name 'BoundCanClass' from external module "C:/Users/ahmefa04/Documents/SourceCode/temp-cd-next/node_modules/@casl/react/dist/types/factory" but cannot be named.ts(4023)导出变量“Can”具有或正在使用来自外部模块“C:/Users/ahmefa04/Documents/SourceCode/temp-cd-next/node_modules/@casl/react/dist/types/factory”的名称“BoundCanClass”但不能命名.ts(4023)
Edit: I exported BoundCanClass in the factory.d.ts file however when I try to use can in my application:编辑:我在 factory.d.ts 文件中导出了 BoundCanClass,但是当我尝试在我的应用程序中使用 can 时:
<Can I="view" on="all">
<Button size="small" onClick={handleUpdateRow}>
Update a user
</Button>
</Can>
I get error:我收到错误:
No overload matches this call.没有过载匹配此调用。 Overload 1 of 2, '(props: BoundCanProps<Ability<AbilityTuple<string, Subject>, { published: boolean; }>>, context?: any): Can<Ability<AbilityTuple<string, Subject>, {...;重载 1 of 2, '(props: BoundCanProps<Ability<AbilityTuple<string, Subject>, { published: boolean; }>>, context?: any): Can<Ability<AbilityTuple<string, Subject>, {... ; }>, true>', gave the following error. }>, true>', 给出了以下错误。 Type '{ children: Element[];输入'{ children: Element[]; I: string;我:字符串; on: string;上:字符串; }' is not assignable to type 'IntrinsicAttributes & (IntrinsicClassAttributes<Can<Ability<AbilityTuple<string, Subject>, { published: boolean; }>, true>> & (Readonly<...> & Readonly<...>))'. }' 不可分配给类型 'IntrinsicAttributes & (IntrinsicClassAttributes<Can<Ability<AbilityTuple<string, Subject>, { published: boolean; }>, true>> & (Readonly<...> & Readonly<...> ))'。 Property 'this' is missing in type '{ children: Element[];类型 '{ children: Element[]; 中缺少属性 'this'; I: string;我:字符串; on: string;上:字符串; }' but required in type 'Readonly<{ I: string; }' 但在类型 'Readonly<{ I: string; 中需要this: AnyRecord;这个:任何记录; field?: string |领域?:字符串| undefined;不明确的; } & BoundCanExtraProps<Ability<AbilityTuple<string, Subject>, { published: boolean; } & BoundCanExtraProps<Ability<AbilityTuple<string, Subject>, { published: boolean; }>>>'. }>>>'。 Overload 2 of 2, '(props: BoundCanProps<Ability<AbilityTuple<string, Subject>, { published: boolean; }>>, context?: any): Component<BoundCanProps<Ability<AbilityTuple<string, Subject>, {...;重载 2 of 2, '(props: BoundCanProps<Ability<AbilityTuple<string, Subject>, { published: boolean; }>>, context?: any): Component<BoundCanProps<Ability<AbilityTuple<string, Subject>, {. ..; }>>, any, any>', gave the following error. }>>, any, any>', 给出了以下错误。 Type '{ children: Element[];输入'{ children: Element[]; I: string;我:字符串; on: string;上:字符串; }' is not assignable to type 'IntrinsicAttributes & (IntrinsicClassAttributes<Component<BoundCanProps<Ability<AbilityTuple<string, Subject>, { published: boolean; }>>, any, any>> & (Readonly<...> & Readonly<...>))'. }' 不可分配给类型 'IntrinsicAttributes & (IntrinsicClassAttributes<Component<BoundCanProps<Ability<AbilityTuple<string, Subject>, { published: boolean; }>>, any, any>> & (Readonly<...> & Readonly <...>))'。 Property 'this' is missing in type '{ children: Element[];类型 '{ children: Element[]; 中缺少属性 'this'; I: string;我:字符串; on: string;上:字符串; }' but required in type 'Readonly<{ I: string; }' 但在类型 'Readonly<{ I: string; 中需要this: AnyRecord;这个:任何记录; field?: string |领域?:字符串| undefined;不明确的; } & BoundCanExtraProps<Ability<AbilityTuple<string, Subject>, { published: boolean; } & BoundCanExtraProps<Ability<AbilityTuple<string, Subject>, { published: boolean; }>>>'.ts(2769) }>>>'.ts(2769)
The problem happens because I
should be used with a
, an
or this
props, to use on
you should use do
instead:出现问题是因为I
应该与a
、 an
或this
道具一起使用on
你应该使用do
来代替:
<Can I="view" a="all">
<Button size="small" onClick={handleUpdateRow}>
Update a user
</Button>
</Can>
or要么
<Can do="view" on="all">
<Button size="small" onClick={handleUpdateRow}>
Update a user
</Button>
</Can>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.