此为官方文档,因为墙的问题,记录下来:

Before spending the time to write your own libdef, we recommend that you look to see if there is already a libdef for the third-party code that you’re addressing. flow-typed is a tool and repository for sharing common libdefs within the Flow community – so it’s a good way to knock out a good chunk of any public libdefs you might need for your project.

However sometimes there isn’t a pre-existing libdef or you have third-party code that isn’t public and/or you really just need to write a libdef yourself. To do this you’ll start by creating a .js file for each libdef you’re going to write and put them in the /flow-typed directory at the root of your project. In these libdef file(s) you’ll use a special set of Flow syntax (explained below) to describe the interfaces of the relevant third-party code.

Declaring A Global Function

To declare a global function that should be accessible throughout your project, use the declare functionsyntax in a libdef file:

flow-typed/myLibDef.js

1
declare function foo(a: number): string;

This tells Flow that any code within the project can reference the foo global function, and that the function takes one argument (a number) and it returns a string.

Declaring A Global Class

To declare a global class that should be accessible throughout your project, use the declare class syntax in a libdef file:

flow-typed/myLibDef.js

1
2
3
4
5
6
declare class URL {
constructor(urlStr: string): URL;
toString(): string; static compare(url1: URL, url2: URL): boolean;
};

This tells Flow that any code within the project can reference the URL global class. Note that this class definition does not have any implementation details – it exclusively defines the interface of the class.

Declaring A Global Variable

To declare a global variable that should be accessible throughout your project, use the declare var syntax in a libdef file:

flow-typed/myLibDef.js

1
declare var PI: number;

This tells Flow that any code within the project can reference the PI global variable – which, in this case, is a number.

Declaring A Global Type

To declare a global type that should be accessible throughout your project, use the declare type syntax in a libdef file:

flow-typed/myLibDef.js

1
declare type UserID = number;

This tells Flow that any code within the project can reference the UserID global type – which, in this case, is just an alias for number.

Declaring A Module

Often, third-party code is organized in terms of modules rather than globals. To write a libdef that declares the presence of a module you’ll want to use the declare module syntax:

1
2
3
declare module "some-third-party-library" {
// This is where we'll list the module's exported interface(s)
}

The name specified in quotes after declare module can be any string, but it should correspond to the same string you’d use to require or import the third-party module into your project. For defining modules that are accessed via a relative require/import path, check out the docs on .js.flow files.

Within the body of a declare module block, you can specify the set of exports for that module. However, before we start talking about exports we have to talk about the two kinds of modules that Flow supports: CommonJS and ES modules.

Flow can handle both CommonJS and ES modules, but there are some relevant differences between the two that need to be considered when using declare module.

Declaring An ES Module

ES modules have two kinds of exports: A named export and a default export. Flow supports the ability to declare either or both of these kinds of exports within a declare module body as follows:

Named Exports

flow-typed/some-es-module.js

1
2
3
4
declare module "some-es-module" {
// Declares a named "concatPath" export
declare export function concatPath(dirA: string, dirB: string): string;
}

Note that you can also declare other things inside the body of the declare module, and those things will be scoped to the body of the declare module – but they will not be exported from the module:

flow-typed/some-es-module.js

1
2
3
4
5
6
7
8
9
10
11
12
declare module "some-es-module" {
// Defines the type of a Path class within this `declare module` body, but
// does not export it. It can only be referenced by other things inside the
// body of this `declare module`
declare class Path {
toString(): string;
}; // Declares a named "concatPath" export which returns an instance of the
// `Path` class (defined above)
declare export function concatPath(dirA: string, dirB: string): Path,
}
Default Exports

flow-typed/some-es-module.js

1
2
3
4
5
6
7
8
9
10
11
declare module "some-es-module" {
declare class URL {
constructor(urlStr: string): URL;
toString(): string; static compare(url1: URL, url2: URL): boolean;
}; // Declares a default export whose type is `typeof URL`
declare export default typeof URL;
}

It is also possible to declare both named and default exports in the same declare module body.

Declaring A CommonJS Module

CommonJS modules have a single value that is exported (the module.exports value). To describe the type of this single value within a declare module body, you’ll use the declare module.exports syntax:

flow-typed/some-commonjs-module.js

1
2
3
4
5
6
declare module "some-commonjs-module" {
// The export of this module is an object with a "concatPath" method
declare module.exports: {
concatPath(dirA: string, dirB: string): string;
};
}

Note that you can also declare other things inside the body of the declare module, and those things will be scoped to the body of the declare modulebut they will not be exported from the module:

flow-typed/some-commonjs-module.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
declare module "some-commonjs-module" {
// Defines the type of a Path class within this `declare module` body, but
// does not export it. It can only be referenced by other things inside the
// body of this `declare module`
declare class Path {
toString(): string;
}; // The "concatPath" function now returns an instance of the `Path` class
// (defined above).
declare module.exports: {
concatPath(dirA: string, dirB: string): Path,
};
}

NOTE: Because a given module cannot be both an ES module and a CommonJS module, it is an error to mix declare export [...] with declare module.exports: ... in the same declare module body.

 
 
 
 

flow 编写flow-typed 定义(官方文档)的更多相关文章

  1. Akka Typed 官方文档之随手记

    ️ 引言 近两年,一直在折腾用FP与OO共存的编程语言Scala,采取以函数式编程为主的方式,结合TDD和BDD的手段,采用Domain Driven Design的方法学,去构造DDDD应用(Dom ...

  2. 006-基于hyperledger fabric1.4( 官方文档)编写第一个应用【外部nodejs调用】

    一.概述 官方原文地址 Writing Your First Application如果对fabric网络的基本运行机制不熟悉的话,请看这里. 注意:本教程是对fabric应用以及如何使用智能合约的简 ...

  3. Swift -- 中文版两大官方文档汇总

    Swift官方文档由CocoaChina翻译小组精心翻译制作而成,目前两本文档中文版已全部完成!在此,我们对所有参与的译者.组织人员以及工作人员表示衷心的感谢!本文为您提供两本文档的在线阅读以及下载! ...

  4. Spring 4 官方文档学习(十一)Web MVC 框架

    介绍Spring Web MVC 框架 Spring Web MVC的特性 其他MVC实现的可插拔性 DispatcherServlet 在WebApplicationContext中的特殊的bean ...

  5. Spring 4 官方文档学习(五)核心技术之SpEL

    题外话 官方文档用evaluate这个单词来描述从表达式中获得实际内容的过程.如果直译的话,应该是评估.估值之类的意思.个人以为翻译成解析更易懂,但parse已经是解析了,为了避免冲突,就只好保留了e ...

  6. Spring 通读官方文档

    Spring 通读官方文档 这部分参考文档涵盖了Spring Framework绝对不可或缺的所有技术. 其中最重要的是Spring Framework的控制反转(IoC)容器.Spring框架的Io ...

  7. OKHttp 官方文档【一】

    最近工作比较忙,文章更新出现了延时.虽说写技术博客最初主要是写给自己,但随着文章越写越多,现在更多的是写给关注我技术文章的小伙伴们.最近一段时间没有更新文章,虽有工作生活孩子占用了大部分时间的原因,但 ...

  8. Lagom 官方文档之随手记

    引言 Lagom是出品Akka的Lightbend公司推出的一个微服务框架,目前最新版本为1.6.2.Lagom一词出自瑞典语,意为"适量". https://www.lagomf ...

  9. 【AutoMapper官方文档】DTO与Domin Model相互转换(上)

    写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...

随机推荐

  1. LA 4287 有相图的强连通分量

    大白书P322 , 一个有向图在添加至少的边使得整个图变成强连通图, 是计算整个图有a个点没有 入度, b 个点没有出度, 答案为 max(a,b) ; 至今不知所云.(求教) #include &l ...

  2. c++第二十一天

    p115~p118: 1.区分int *p[4];和int (*p)[4];.前者是整型指针的数组,后者是指向含有4个整数的数组. 2.规避上述问题的方法就是:使用 auto和 decltype. 3 ...

  3. PHP设计模式_单例模式

    了解 单例设计模式用于限制特定对象只能被实例化创建一次,有且只有一个此类型的资源.例如,通过数据库句柄到数据库的连接是独占的.您希望在应用程序中共享数据库句柄,因为在保持连接打开或关闭时,它是一种开销 ...

  4. P1757 通天之分组背包 / hdu1712 ACboy needs your help (分组背包入门)

    P1757 通天之分组背包 hdu1712 ACboy needs your help hdu1712题意:A[i][j]表示用j天学习第i个课程能够得到A[i][j]的收益,求m天内获得的收益最大值 ...

  5. 20145301《网络对抗》shellcode注入&Return-to-libc攻击深入

    20145301<网络对抗>shellcode注入&Return-to-libc攻击深入 Shellcode注入 shellcode是什么? Shellcode是指能完成特殊任务的 ...

  6. 20145301《网络对抗》Exp2 后门原理与实践

    20145301<网络对抗>Exp2 后门原理与实践 基础问题回答 例举你能想到的一个后门进入到你系统中的可能方式? 系统或者某些软件自身留下的后门. 钓鱼网站等非正规网站上捆绑下载 例举 ...

  7. [数据库] - org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection

    MySQL的驱动改名了,如果使用原来的com.mysql.jdbc.Driver 那么会提醒驱动不正常了,那么新的MySQL驱动名为:com.mysql.cj.jdbc.Driver 之后还报错,如题 ...

  8. tomcat跟目录下work文件夹的作用

    work目录只是tomcat的工作目录,也就是tomcat把jsp转换为class文件的工作目录. jsp,tomcat的工作原理:当浏览器访问某个jsp页面时,tomcat会在work目录里把这个j ...

  9. Tornado教程目录

    第一章:引言 第二章:表单和模板 第三章:模板扩展 第四章:数据库 第五章:异步Web服务 第六章:编写安全应用 第七章:外部服务认证 第八章:部署Tornado

  10. 运用模型绑定和web窗体显示和检索数据(Retrieving and displaying data with model binding and web forms)

    原文 http://www.asp.net/web-forms/overview/presenting-and-managing-data/model-binding/retrieving-data ...