如何为一组匹配文件声明导出类型?
How to declare an export type for a set of matching files?
我有一个文件夹 ./plugins
。插件具有以下接口:
type PluginType = () => Promise<(ad: AdType) => TargetingParameterType>;
为了使用 Flow,我需要将 PluginType
导入到每个插件脚本中并声明导出类型,例如这就是我现在正在做的事情:
import type {
PluginType
} from './types';
const myPlugin: PluginType = async () => {
return (ad) => {
return {};
};
};
export default myPlugin;
这种方法的问题是:
- 它需要创建一个中间变量(我找不到内联方式来注释
export default
类型)
- 要求每个
./plugins/*.js
文件中都包含此注释。
有没有办法配置 Flow 以将 PluginType
类型应用于 ./plugins/*.js
文件夹中的所有文件,而无需向每个文件添加类型声明?
您可以使用 ".flowconfig
-style" declarations 创建项目范围的类型声明。在您的 .flowconfig
中,添加:
[libs]
decls/
然后创建目录 decls
,并在其中创建一个名为 plugins.js
的文件,其中包含:
declare type PluginType = () => Promise<(ad: AdType) => TargetingParameterType>;
您可能还需要在此文件中包含 PluginType
所依赖的类型。
According to the documentation:
It is similarly useful to declare types. Like other declarations, type declarations can also be made visible to all modules in a project.
为避免创建中间变量,您可以使用 typecast 语法:
export default (async () => {
return (ad) => {
return {};
};
}: PluginType);
我有一个文件夹 ./plugins
。插件具有以下接口:
type PluginType = () => Promise<(ad: AdType) => TargetingParameterType>;
为了使用 Flow,我需要将 PluginType
导入到每个插件脚本中并声明导出类型,例如这就是我现在正在做的事情:
import type {
PluginType
} from './types';
const myPlugin: PluginType = async () => {
return (ad) => {
return {};
};
};
export default myPlugin;
这种方法的问题是:
- 它需要创建一个中间变量(我找不到内联方式来注释
export default
类型) - 要求每个
./plugins/*.js
文件中都包含此注释。
有没有办法配置 Flow 以将 PluginType
类型应用于 ./plugins/*.js
文件夹中的所有文件,而无需向每个文件添加类型声明?
您可以使用 ".flowconfig
-style" declarations 创建项目范围的类型声明。在您的 .flowconfig
中,添加:
[libs]
decls/
然后创建目录 decls
,并在其中创建一个名为 plugins.js
的文件,其中包含:
declare type PluginType = () => Promise<(ad: AdType) => TargetingParameterType>;
您可能还需要在此文件中包含 PluginType
所依赖的类型。
According to the documentation:
It is similarly useful to declare types. Like other declarations, type declarations can also be made visible to all modules in a project.
为避免创建中间变量,您可以使用 typecast 语法:
export default (async () => {
return (ad) => {
return {};
};
}: PluginType);