简体   繁体   English

无法在 JS 中使用来自 TypeScript 的“接口”声明(React Native 测试)

[英]Cannot use "interface" declarations from TypeScript in JS (React Native tests)

I have a ReactNative app and I am trying to write a test using Jest.我有一个 ReactNative 应用程序,我正在尝试使用 Jest 编写测试。 My test need to use classes from a native component (react-native-nfc-manager) and a class I need is declared like this我的测试需要使用来自本机组件(react-native-nfc-manager)的类和我需要的 class 声明如下

  export interface TagEvent {
    ndefMessage: NdefRecord[];
    maxSize?: number;
    type?: string;
    techTypes?: string[];
    id?: number[];
  }

and if I try to use this definition directly like如果我尝试直接使用这个定义,比如

const event = new TagEvent();

I get this error我收到这个错误

TypeError: _reactNativeNfcManager.default is not a constructor TypeError:_reactNativeNfcManager.default 不是构造函数

I am coming from Java worlds so I think to myself - of course it cannot instantiate an interface, it needs a class so I write a test instance for this interface:我来自 Java 世界,所以我想我自己 - 当然它不能实例化一个接口,它需要一个 class 所以我为此接口编写了一个测试实例:

class TestTagEvent implements TagEvent {
    ndefMessage: NdefRecord[] = []
    maxSize?: number;
    type?: string;
    techTypes?: string[];
    id?: number[];
}

But the problem seem to be different since it does not work either:但问题似乎有所不同,因为它也不起作用:

TypeError: _TestTagEvent.TestTagEvent is not a constructor TypeError:_TestTagEvent.TestTagEvent 不是构造函数

What am I missing?我错过了什么?

You can't construct an object using an interface.您不能使用接口构造 object。 An interface is just a set of properties that can be used to define required/available properties of variables.接口只是一组属性,可用于定义变量的必需/可用属性。

To use the interface to initialize a variable, you want to do this:要使用接口初始化变量,您需要执行以下操作:

const eventVar: TagEvent;

If you want to use TagEvent as a instantiable object, you need to define it as a class, not an interface.如果要将 TagEvent 用作可实例化的 object,则需要将其定义为 class,而不是接口。 Something like this, where TagEventProps is the TagEvent interface you defined:像这样,其中TagEventProps是您定义的TagEvent接口:

class TagEvent {
  constructor(props: TagEventProps) {
    this.ndefMesssage = props.nDefMessage;
    //etc...
  }
}

After further experiments problem has been solved by adding 'export' to the class.在进一步的实验之后,通过向 class 添加“导出”来解决问题。 Admittedly, the original error description is absolutely not matching the root cause.诚然,最初的错误描述绝对不符合根本原因。

export class TestTagEvent implements TagEvent {导出 class TestTagEvent 实现 TagEvent {

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

相关问题 在TypeScript中使用import / require来获取接口声明 - Use import/require in TypeScript to get interface declarations React.js的新手无法确定正确的Typescript接口 - Newbie to React.js cannot determine proper Typescript Interface 不能在 typescript 中使用来自 js 的 function - cannot use function from js in typescript useState with interface- react native typescript - useState with interface- react native typescript 如何使用 React 和 TypeScript 将实现与类型声明分开? - How can I separate a implementations from type declarations with React and TypeScript? 常规 JS 中的 TypeScript 检查和声明 - TypeScript checks and declarations in regular JS 在 Typescript/React 中使用一种或另一种接口类型 - Use one or another interface type in Typescript/React 运行测试时无法从“node_modules/react-native/jest/setup.js”中找到模块“@babel/runtime/helpers/interopRequireDefault” - Cannot find module '@babel/runtime/helpers/interopRequireDefault' from 'node_modules/react-native/jest/setup.js' when I run tests 找不到'react-native-implementation.js'中的模块 - Cannot find module from 'react-native-implementation.js' 在 React Typescript 项目中使用来自 JS 库的 window object - Use window object from JS lib in React Typescript project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM