简体   繁体   English

Typescript 将 can 绑定到能力实例时出错

[英]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应该与aanthis道具一起使用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>

Docs reference 文档参考

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在TypeScript中使用静态方法的实例属性时出错 - Error when using instance property from static method in TypeScript 创建实例时,打字稿类装饰器可以是可选的吗? - Can typescript class decorators be optional when creating an instance? 当值可以是函数或空时打字稿错误 - Typescript error when value can be function or null 打字稿不能识别错误的子类的实例? - Typescript is not identifying Instance of subclass of Error? React/Typescript - 使用 ref 时出现 typescript 错误 - 类型 '(instance: HTMLInputElement | null) =&gt; void' 上不存在属性 'current' - React/Typescript - when using ref I get typescript error - Property 'current' does not exist on type '(instance: HTMLInputElement | null) => void' 使用@nestjs/mongoose 时,如何将实例/静态方法添加到 ZCCADCDEDB567ABAE643E15DCF0974E503Z 架构? (打字稿问题) - How can I add instance/static methods to Mongoose schema when using @nestjs/mongoose? (TypeScript Issues) 打字稿:当函数可以是多种函数类型时,函数参数出错 - Typescript: Error on function argument when the function can be of multiple function types 如何返回类的实例或Typescript中的错误 - how to return either instance of a class or an error in Typescript CustomError typescript,组件内部错误的错误实例 - CustomError typescript, wrong instance of the error inside the component 在“typescript”中克隆 object 时如何解决类型错误? - How can I solve type error when cloning an object in `typescript`?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM