【问题标题】:Property 'subscriptionClient' is private and only accessible within class 'WebSocketLink'.ts(2341)属性 'subscriptionClient' 是私有的,只能在类 'WebSocketLink'.ts(2341) 中访问
【发布时间】:2021-01-10 11:38:30
【问题描述】:
"@apollo/client": "^3.0.2"
"@types/node": "^14.0.20",
"@types/react": "^16.9.49",
"typescript": "^4.0.3"

所以我在尝试使用以下代码将文件转换为打字稿时遇到上述错误:

lib/withData.ts

  import { WebSocketLink } from '@apollo/client/link/ws';

  const wsLink = isBrowser ? new WebSocketLink({
    uri: process.env.NODE_ENV === 'development' ? endpointWS : prodEndpointWS,
    options: {
      reconnect: true,
      lazy: true
    }
  }) : null;

  if (wsLink) { 
    wsLink.subscriptionClient.on("connecting", () => {
      console.log("connecting");
    });
    
    wsLink.subscriptionClient.on("connected", () => {
      console.log("connected");
    });
    
    wsLink.subscriptionClient.on("reconnecting", () => {
      console.log("reconnecting");
    });
    
    wsLink.subscriptionClient.on("reconnected", () => {
      console.log("reconnected");
    });
    
    wsLink.subscriptionClient.on("disconnected", () => {
      console.log("disconnected");
    });
    
    wsLink.subscriptionClient.maxConnectTimeGenerator.duration = () =>
      wsLink.subscriptionClient.maxConnectTimeGenerator.max;
  }

我该如何解决这个问题?我的部分回购位于此处:https://github.com/TheoMer/next_apollo

【问题讨论】:

  • WebSocketLink 的作者决定使用 TS private 关键字将该属性标记为 private,以防止您在没有 TS 错误的情况下使用它。 private 修饰符在运行时没有影响,纯粹影响静态检查。克服这个问题的唯一实用方法是使用各种方法(例如类型断言)来抑制错误。但是,private 的使用可能会传达作者的意图,您应该在使用该成员之前三思而后行。

标签: typescript apollo-client


【解决方案1】:

我通过将 wsLink 声明为:

let wsLink: any;

【讨论】:

    猜你喜欢
    • 2016-12-14
    • 2017-02-23
    • 2017-08-25
    • 2017-08-27
    • 2019-06-17
    • 2018-01-01
    • 2019-06-19
    相关资源
    最近更新 更多