React Relay(react-relay)与Typescript抛出TypeError:对象原型可能只是一个Object或null:undefined

时间:2017-08-06 19:11:47

标签: javascript reactjs typescript relayjs react-relay

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

每当我运行yarn start时,这就是我所看到的:

Relay.Mutation instantiation error

这个错误对我来说真的没有意义: TypeError: Object prototype may only be an Object or null: undefined

我只是试图实例化我自己的扩展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

更新:反应中继的打字目前无效&gt; 1.x的请参阅thread on github for this issue。我还用解决方法更新了我的回购。

1 个答案:

答案 0 :(得分:1)

问题是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"
  ]
}