如何在socket.io-client中定义AUTH属性的类型

发布于 2025-01-25 15:10:53 字数 1699 浏览 3 评论 0 原文

问题

我正在使用socket.io-client和tyscript。

创建套接字实例时,我会在套接字选项的身份验证中设置一个令牌属性,但是使用时会发生类型错误。

我该如何解决?


  • “ socket.io-client”:“ 4.5.0”
  • “ typescript”:“ 4.6.4”

import { io, Socket } from "socket.io-client";
import { ServerToClientEventsInterface, ClientToServerEventsInterface } from "../../../backend/socket/interface/socketEventsInterface";
const socket: Socket<ServerToClientEventsInterface, ClientToServerEventsInterface> = io({
    auth: (cb: any) => {
        cb({ token: localStorage.token })
    }
});

socket.on('created', () => {
    console.log("token", socket.auth.token);
});

错误

ts2339:属性'token'在type'{[key:string]:any; } | ((cb :(数据:对象)=&gt; void)=&gt; void)'。 属性“令牌”不存在于类型上'(cb :( data:object)=&gt; void)=&gt; void'。


的定义

export interface SocketOptions {
    /**
     * the authentication payload sent when connecting to the Namespace
     */
    auth: {
        [key: string]: any;
    } | ((cb: (data: object) => void) => void);
}

插座选项参考文献文档


socket.on('created', () => {
    const tmp: any = socket.auth;
    console.log("token", tmp.token);
});

让我告诉你,我说的英语不能很好。 对不起,如果这是一个奇怪的句子。 请指出您不理解的任何句子,我将尽力纠正它们。

如果您能回答,我会很感激。 谢谢。

Question

I am using socket.io-client with typescript.

When creating a socket instance, I set a token property in the auth of the Socket options, but an type error occurs when using it.

How can I resolve this?


  • "socket.io-client": "4.5.0"
  • "typescript": "4.6.4"

source

import { io, Socket } from "socket.io-client";
import { ServerToClientEventsInterface, ClientToServerEventsInterface } from "../../../backend/socket/interface/socketEventsInterface";
const socket: Socket<ServerToClientEventsInterface, ClientToServerEventsInterface> = io({
    auth: (cb: any) => {
        cb({ token: localStorage.token })
    }
});

socket.on('created', () => {
    console.log("token", socket.auth.token);
});

source capture

error

TS2339: Property 'token' does not exist on type '{ [key: string]: any; } | ((cb: (data: object) => void) => void)'.
Property 'token' does not exist on type '(cb: (data: object) => void) => void'.


the definition of Socket options

export interface SocketOptions {
    /**
     * the authentication payload sent when connecting to the Namespace
     */
    auth: {
        [key: string]: any;
    } | ((cb: (data: object) => void) => void);
}

Reference document


tried

I've confirmed that the error can be avoided by the following method, but I don't think it's the right way to go.

socket.on('created', () => {
    const tmp: any = socket.auth;
    console.log("token", tmp.token);
});

let me tell you that I can't speak English well.
I'm sorry if it's a strange sentence.
Please point out any sentences that you do not understand, and I will do my best to correct them.

I would be very grateful if you could answer.
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文