如何使用 CommonJS 导出流接口?

How can I export a flow interfaces using CommonJS?

我不确定我的构建是否好,但在我的package.json中,我有

"scripts": {
    "build": "flow-remove-types src/ -d build/",

我有一个 types/sentence.js,它有:

// @flow

type SentenceObject = {
  foo: number,
  bar: boolean,
  baz: string,
};

module.exports = {
  SentenceObject
};

在我的库文件中,我有:

const { SentenceObject } = require('../interfaces/sentence');

当我做 yarn build 时的问题是:

src/interfaces/sentence.js
 ↳ Syntax Error: Unexpected token, expected = (3:27)
   2:  export type SentenceObject {

我做错了什么?

好像有. Instead you can use export/import as suggested in docs。所以在你的情况下它可能是这样的:

types/sentence.js:

// @flow
export type SentenceObject = {
  foo: number,
  bar: boolean,
  baz: string,
};

注意 export 关键字。

并且在你的库文件中你可以使用 import type {...} from '':

import type { SentenceObject } from '../interfaces/sentence';