简体   繁体   English

在Flow中导入TypeScript模块

[英]Importing TypeScript modules in Flow

We are in the process of migrating a front-end React application from Flow to TypeScript. 我们正在将前端React应用程序从Flow迁移到TypeScript。

I have the Webpack and TS config setup to load TypeScript files and it all compiles and builds fine. 我有Webpack和TS配置设置来加载TypeScript文件,并且所有这些都可以编译和构建。

The flow check is performed separately (is not part of the build process) until all the files are switched to TypeScript but this is failing with errors of this nature: 流检查是单独执行的(不是构建过程的一部分),直到所有文件都切换到TypeScript,但这会失败,并出现以下性质的错误:

src/SomeFlowModule.js:5
  5: import TypeScriptModule from './TypeScriptModule';
                           ^^^^^^^^^^^^^ ./TypeScriptModule. Required module not found

Is there a way to make Flow aware of imported TypeScript files and perhaps just treat them as any ? 有没有一种方法可以使Flow知道导入的TypeScript文件,也许只是将它们视为any

The solution I came up with was to add a declaration for TypeScript files: 我想出的解决方案是为TypeScript文件添加一个声明:

declare module TsStub {
  declare var exports: any;
}

You'll need to reference this stub in your .flowconfig so Flow knows about it under the [libs] key. 您需要在.flowconfig中引用此存根,以便Flow在[libs]键下知道它。 Eg if it was saved in interfaces/TsStub.js I would just add this to .flowconfig: 例如,如果将其保存在interfaces/TsStub.js ,则将其添加到.flowconfig中:

[libs]
interfaces/

And then reference the stub in my .flowconfig: 然后在我的.flowconfig中引用存根:

module.name_mapper='^[./a-zA-Z0-9$_-]+\.\(ts\|tsx\)$' -> 'TsStub'

I now need to import ts files with their extension (eg import myTsModule from 'my-ts-module.ts' ) but it does compile fine. 现在,我需要导入带有扩展名的ts文件(例如, import myTsModule from 'my-ts-module.ts' ),但是编译起来还不错。

When I get around to converting the flow file to TS I just need to fix the extensions, but the compilers help you with this. 当我开始将流文件转换为TS时,我只需要修复扩展名,但是编译器会为此提供帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM