简体   繁体   中英

Importing TypeScript modules in Flow

We are in the process of migrating a front-end React application from Flow to TypeScript.

I have the Webpack and TS config setup to load TypeScript files and it all compiles and builds fine.

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:

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 ?

The solution I came up with was to add a declaration for TypeScript files:

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. Eg if it was saved in interfaces/TsStub.js I would just add this to .flowconfig:

[libs]
interfaces/

And then reference the stub in my .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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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