如何声明REST API的流类型

时间:2018-02-13 14:32:00

标签: javascript rest flowtype

===== EDIT =====

感谢@kalley我更新了我的声明文件并取得了一些进展。我不再有linting错误,但我仍然无法识别我导入它们的类型。我更新了上面文件的内容

=====结束编辑=====

我正在尝试为我正在处理的项目使用的REST API创建流类型。

我遇到了一个问题:  当我使用新类型

编辑源时,看不到类型

这是包含类型(src / flow-typed / API.js)

的文件
// @flow

declare module "API" {
  //  type Error = {code:number, message:string};
  declare export type ImageJSON = {
    url: string,
    reference: string
  };

  declare export type CategoryJSON = {
    reference: number,
    name: string,
    slug: string,
    icon: ImageJSON,
    children: [],
    created_at: string
  };

  declare export type CompanyJSON = {
    reference: number,
    name: string,
    slug: string,
    email: string,
    occupational_medicine: string,
    psychology_of_work: string,
    logo: ImageJSON,
    categories: Array<CategoryJSON>,
    created_at: string
  };

  declare export type GetCompanyResponse = {
    error: Error,
    company: CompanyJSON
  };

  // declare export default GetCompanyResponse
}

我以这种方式导入文件

import typeof { GetCompanyResponse } from "../flow-typed/API";

我收到以下流量错误:

[flow] Named import from module `../flow-typed/WazzaAPI` (This module only has a default export. Did you mean `import GetCompanyResponse from ...`?)

如果我像这样导入文件     从“../ flow-typed / API”;

导入typeof GetCompanyResponse

然后在代码中我得到一个错误,说GetCompanyResponse没有错误成员:

.then((response: GetCompanyResponse) => {
    //check to see if there is an error
    if (response.error.code !== ERROR_CODE.noError) {

错误:

[flow] property `error` (Property not found in exports)

赞赏任何指针

0 个答案:

没有答案
相关问题