You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React中使用SignalR报错:Property 'hubConnection'不存在于JQueryStatic类型

React项目中SignalR Property 'hubConnection' does not exist on type 'JQueryStatic' 错误解决方法

以下是几种可行的解决方式:

  • 扩展jQuery类型声明
    TypeScript报错是因为默认的jQuery类型定义里没有hubConnection方法,你可以手动扩展它:

    1. 在项目中创建一个类型声明文件(比如src/types/signalr.d.ts)
    2. 在文件中添加以下代码:
    interface JQueryStatic {
      hubConnection(url?: string, options?: Record<string, any>): any;
    }
    
    1. 确保tsconfig.json的include数组包含这个文件路径,比如:
    {
      "include": ["src/**/*", "src/types/**/*.d.ts"]
    }
    
  • 调整SignalR引入顺序
    确保SignalR在jQuery挂载到window之后加载,可将require替换为import保证加载顺序:

    import $ from 'jquery';
    window.jQuery = $;
    import 'ms-signalr-client';
    
    const connection = $.hubConnection("http://x.x.x.x/signalr", { useDefaultPath: false });
    
  • 临时类型断言(应急方案)
    如果需要快速绕过类型检查,可以使用类型断言将$转为any类型:

    const connection = ($ as any).hubConnection("http://x.x.x.x/signalr", { useDefaultPath: false });
    

    注意:这种方式会失去TypeScript的类型校验,不建议长期使用。

内容的提问来源于stack exchange,提问作者Telirw

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 15:27:49