Javascript 这个流程是什么;多态类型而非交叉类型“;误差平均值?

Javascript 这个流程是什么;多态类型而非交叉类型“;误差平均值?,javascript,flowtype,next.js,Javascript,Flowtype,Next.js,这个错误是什么意思 应为多态类型,而不是交叉点类型的任何成员。 详细信息: 下面导出行中的文档引发错误。我不明白为什么会这样,也不知道我能做些什么来修复它。(我试着在流程文档中查找,但仍然无法找到答案。) 首先,我想指出,您提供的代码需要一些更新才能使用Flow v0.53.0或更高版本。该版本对React的流注释工作方式进行了重大更改。尤其是组件类更改为采用两个类型参数,而不是三个。我在回答中对代码片段做了必要的修改 在最新版本的Flow(v0.59.0)中,错误消息报告了更详细的信息: E

这个错误是什么意思

应为多态类型,而不是交叉点类型的任何成员。


详细信息:

下面导出行中的
文档
引发错误。我不明白为什么会这样,也不知道我能做些什么来修复它。(我试着在流程文档中查找,但仍然无法找到答案。)


首先,我想指出,您提供的代码需要一些更新才能使用Flow v0.53.0或更高版本。该版本对React的流注释工作方式进行了重大更改。尤其是
组件
类更改为采用两个类型参数,而不是三个。我在回答中对代码片段做了必要的修改

在最新版本的Flow(v0.59.0)中,错误消息报告了更详细的信息:

Error: react.js:7
  7: export default class IntlDocument extends Document {
                                               ^^^^^^^^ identifier `Document`. Expected polymorphic type instead of any member of intersection type
  7: export default class IntlDocument extends Document {
                                               ^^^^^^^^ intersection
  Member 1:
   15:   declare export default Class<Component<*, *>> & {
                                ^^^^^^^^^^^^^^^^^^^^^^ class type: type application of Component. See lib: types/next-document.js:15
  Error:
    9:     const props = await super.getInitialProps(context)
                                     ^^^^^^^^^^^^^^^ property `getInitialProps`. Property not found in
    9:     const props = await super.getInitialProps(context)
                               ^^^^^ statics of React$Component
  Member 2:
                                                         v
   15:   declare export default Class<Component<*, *>> & {
   16:     getInitialProps: (ctx: Context) => Promise<*>;
   17:     renderPage(cb: Function): void;
   18:   };
         ^ object type. See lib: types/next-document.js:15
  Error:
    7: export default class IntlDocument extends Document {
                                                 ^^^^^^^^ identifier `Document`. Expected polymorphic type instead of
                                                         v
   15:   declare export default Class<Component<*, *>> & {
   16:     getInitialProps: (ctx: Context) => Promise<*>;
   17:     renderPage(cb: Function): void;
   18:   };
         ^ object type. See lib: types/next-document.js:15


Found 1 error

我能理解为什么原作者没有这样做。该类型定义文件导入
组件
类型,如下所示:

import type {Component} from 'react';
types/next-document.js:26
 26:   declare export default class Document<Props, State = void> extends Component<Props, State> {
                                                                          ^^^^^^^^^ Component. type referenced from value position
  2:   import type {Component} from 'react';
                    ^^^^^^^^^ type Component
在流中,类是具有相同名称的值和类型。您需要该值来创建子类,因为子类在运行时存在,而类型在运行时不存在。该导入引入了
组件
类型,但没有值,如果值不在范围内,则流将不允许您声明子类。如果您尝试,将出现如下错误:

import type {Component} from 'react';
types/next-document.js:26
 26:   declare export default class Document<Props, State = void> extends Component<Props, State> {
                                                                          ^^^^^^^^^ Component. type referenced from value position
  2:   import type {Component} from 'react';
                    ^^^^^^^^^ type Component
types/next document.js:26
26:声明导出默认类文档扩展组件{
^^^^^^^^^组件。从值位置引用的类型
2:从“react”导入类型{Component};
^^^^^^^^^类型组件
原始作者仅导入了类型,因为类型定义文件不允许导入值-它们只能导入类型。作者发现它们无法正确声明子类,于是求助于类型联合解决方案

React的类型定义(内置于流中,请参见)将某些类型放入全局命名空间。这是一种解决方法,类型定义作者有时会因为在类型定义文件中导入内容时出现问题而使用这种方法。惯例是使用大写的模块名称和美元符号作为全局作用域类型的前缀。因此,您可以在类型定义文件中使用
React$Component
type不要导入它;这就是我在上面的声明中所做的

declare export default class Document<Props, State = void> extends React$Component<Props, State> {
  static getInitialProps(ctx: Context): Promise<Props>;
  static renderPage(cb: Function): void;
}
import type {Component} from 'react';
types/next-document.js:26
 26:   declare export default class Document<Props, State = void> extends Component<Props, State> {
                                                                          ^^^^^^^^^ Component. type referenced from value position
  2:   import type {Component} from 'react';
                    ^^^^^^^^^ type Component