为什么任何导入都修复了 --isolatedModules 错误? [英] Why is --isolatedModules error fixed by any import?

查看:51
本文介绍了为什么任何导入都修复了 --isolatedModules 错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个 create-react-app 打字稿项目中,我试图写这个只是为了快速测试一些东西:

In a create-react-app typescript project, I tried to write this just to test some stuff quickly:

// experiment.test.ts
it('experiment', () => {
  console.log('test');
});

但是它给了我以下错误,在it下面有一个红色的波浪线:

But it gives me the following error, with a red squiggly beneath it:

当提供--isolatedModules"标志时,所有文件都必须是模块.

All files must be modules when the '--isolatedModules' flag is provided.

但是,如果我将文件更改为以下内容,那么一切显然都很好(当然,未使用的导入除外):

However, if I change the file to the following, then everything apparently is fine (except for the unused import of course):

// experiment.test.ts
import { Component} from 'react'; // literally anything, don't even have to use it

it('test', () => {
  console.log('test');
});

为什么?这里发生了什么?--isolatedModules 实际上是什么意思/做什么?

Why? What is happening here? What does --isolatedModules actually mean/do?

推荐答案

Typescript 将没有导入/导出的文件视为遗留脚本文件.由于这些文件不是模块,它们的任何定义都已合并到全局命名空间中.isolatedModules 禁止此类文件.

Typescript treats files without import/exports as legacy script files. As such files are not modules and any definitions they have get merged in the global namespace. isolatedModules forbids such files.

向文件添加任何导入或导出使其成为模块并且错误消失.

Adding any import or export to a file makes it a module and the error disappears.

此外,export {} 是一种方便的方法,无需导入任何内容即可使文件成为模块.

Also export {} is a handy way to make a file a module without importing anything.

这篇关于为什么任何导入都修复了 --isolatedModules 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆