本文介绍了开玩笑:类型错误:无法读取未定义的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试我的 React 类,该类具有 import dotnetify from dotnetify"; import.这工作正常,但 Jest 说,dotnetify 是未定义的.如果我更改为 const dotnetify = require("dotnetify");,Jest 通过测试,但这是愚蠢的解决方法.怎么解释Jest,那个dotnetify不是undefined?

Im trying to test my React class, that has import dotnetify from "dotnetify"; import. That works fine, but Jest says, that dotnetify is undefined. If i change to const dotnetify = require("dotnetify");, Jest passes test, but this is silly workaround. How to explain Jest, that dotnetify is not undefined?

比你提前.

推荐答案

这无法向 Jest解释",它真的是未定义的.

This cannot be 'explained' to Jest, it's really undefined.

在 TypeScript 中有几种处理 CommonJS 模块的方法.如这个答案中所述,只有在使用 esModuleInterop (allowSyntheticDefaultImports) 编译器选项.

There are several ways to handle CommonJS modules in TypeScript. As explained in this answer, there will be default import in CommonJS packge only if synthetic imports were enabled with esModuleInterop (allowSyntheticDefaultImports) compiler option.

否则应该这样做:

import * as dotnetify from "dotnetify";

或者使用特定于 TypeScript 的语法:

Or with TypeScript-specific syntax:

import dotnetify = require("dotnetify")

这篇关于开玩笑:类型错误:无法读取未定义的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 06:02