javascript - React Relay (react-relay) 与 Typescript 抛出 TypeError : Object prototype may only be an Object or null: undefined

标签 javascript reactjs typescript relayjs react-relay

当我尝试实例化一个扩展Relay.Mutation的类时,我遇到了一个非常奇怪的react-relay类型错误。我使用 create-react-app my-app --scripts-version=react-scripts-ts 创建了一个简单的 Typescript 项目来演示该问题。

每当我运行yarn start时,我都会看到以下内容:

Relay.Mutation instantiation error

这个错误对我来说并没有什么意义: 类型错误:对象原型(prototype)只能是一个对象或 null:未定义

我只是想实例化我自己的扩展 Relay.Mutation 类的新实例。总的来说,我对 React Relay 世界和 ES6/Typescript 还很陌生,所以我可能错过了一些愚蠢的东西。

在这个简单的项目中,我直接使用 DefinitelyTyped repository 中定义的示例类。 .

我不应该能够像这样使用这个类吗:

const atm = new AddTweetMutation({ text: 'asdf', userId: '1123' });

这是 AddTweetMutation.tsx 类的样子:

import * as Relay from 'react-relay';

interface Props {
  text: string;
  userId: string;
}

interface State {
}

export default class AddTweetMutation extends Relay.Mutation<Props, State> {

  public getMutation() {
    return Relay.QL`mutation{addTweet}`;
  }

  public getFatQuery() {
    return Relay.QL`
            fragment on AddTweetPayload {
                tweetEdge
                user
            }
        `;
  }

  public getConfigs() {
    return [{
      type: 'RANGE_ADD',
      parentName: 'user',
      parentID: this.props.userId,
      connectionName: 'tweets',
      edgeName: 'tweetEdge',
      rangeBehaviors: {
        '': 'append',
      },
    }];
  }

  public getVariables() {
    return this.props;
  }
}

这是整个 Hello.tsx React 组件:

import * as React from 'react';
import AddTweetMutation from '../mutations/AddTweetMutation';

export interface Props {
  name: string;
  enthusiasmLevel?: number;
}

class Hello extends React.Component<Props, {}> {
  render() {

    const atm = new AddTweetMutation({ text: 'asdf', userId: '1123' });
    console.log(atm);

    const { name, enthusiasmLevel = 1 } = this.props;

    if (enthusiasmLevel <= 0) {
      throw new Error('You could be a little more enthusiastic. :D');
    }

    return (
      <div className="hello">
        <div className="greeting">
          Hello {name + getExclamationMarks(enthusiasmLevel)}
        </div>
      </div>
    );
  }
}

export default Hello;

// helpers

function getExclamationMarks(numChars: number) {
  return Array(numChars + 1).join('!');
}

这就是我的 package.json 的样子:

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@types/jest": "^20.0.6",
    "@types/node": "^8.0.19",
    "@types/react": "^16.0.0",
    "@types/react-dom": "^15.5.2",
    "@types/react-relay": "^0.9.13",
    "react": "^15.6.1",
    "react-dom": "^15.6.1",
    "react-relay": "^1.1.0",
    "react-scripts-ts": "2.5.0"
  },
  "devDependencies": {},
  "scripts": {
    "start": "react-scripts-ts start",
    "build": "react-scripts-ts build",
    "test": "react-scripts-ts test --env=jsdom",
    "eject": "react-scripts-ts eject"
  }
}

Here's the project to github

更新:输入目前对于 React-relay > 1.x 无效。请参阅thread on github for this issue 。我还用解决方法更新了我的存储库。

最佳答案

问题在于 react-relay@1.1.0 已更改其 API,并且 @types/react-relay@0.9.13 已过时。

TypeScript 根据可用的类型(类型定义)静态分析您的代码。因此,即使您的 @types/react-relay@0.9.13 已经过时,TypeScript 也不知道,只是根据它采取行动。

要解决此问题,您可以:

对于最后一个选项,请执行以下操作:

// custom-typings/react-relay.d.ts
declare module 'react-relay'

// tsconfig.json
{
  "include": [
    "custom-typings"
  ]
}

关于javascript - React Relay (react-relay) 与 Typescript 抛出 TypeError : Object prototype may only be an Object or null: undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45535641/

相关文章:

javascript - SharePoint JavaScript 执行一个又一个异步函数

facebook - React-tools JSX 编译嵌套文件夹错误

javascript - 在nodejs中获取文件(word、excel、ppt)元数据信息

angular - 如何在我打字时垂直自动调整 ionic 输入场的大小

javascript - 与 useEffect 一起使用时如何防止 useCallback 触发(并遵守 eslint-plugin-react-hooks)?

javascript - 如何从 Node.js 应用程序连接 AWS elasticache redis?

javascript - 同步发出多个ajax请求

javascript - JS中如何用字符串调用数组名

javascript - 在函数中调用 setState(),并在状态改变后继续下一个同级函数——这可能吗?

如果声明参数类型,TypeScript 函数实现会将参数类型降级为 `any`