将流类型添加到现有功能

时间:2017-09-07 10:18:10

标签: flowtype

如果我从第三方库获得了一个函数,我想向它添加流类型。

例如

 import foo from 'foo';

 // normal call without flowtype check, because foo doesn't use flowtype when declaration
 foo('hello')

 // I want to add param types to function foo
 type FooParamType = 
   | 'only'
   | 'those'
   | 'words'
   | 'are'
   | 'valid'

// I want to re-assign param types to foo
foo(a: FooParamType)

// now I can use foo with flowtype
foo('only')  // good
foo('are')  // good
foo('what') // bad

函数foo已经在其他地方声明了。如何将类型添加到函数中?

1 个答案:

答案 0 :(得分:2)

查看flow-typed project,其中包含数百个第三方库的流式定义。

如果您尝试输入的库在该项目中不存在,您仍然可以按照他们用来创建自己的类型的模式。

在您的情况下,一种可能性是将foo.js添加到/flow/lib

declare module 'foo' {
  declare type FooParamType = 
   | 'only'
   | 'those'
   | 'words'
   | 'are'
   | 'valid'

  declare function exports(a: FooParamType): any;
}

您只需在[libs] section of your .flowconfig.

下添加flow/lib即可
相关问题