How to use socket.IO client type definitions for Typescript?

不羁岁月 提交于 2020-05-15 05:15:40

问题


I have installed the Typescript definitions for a SocketIO client using

npm install @types/socket.io-client

But in VS Code I still get type errors:

let socket: SocketIOClientStatic = io() 

Type 'Socket' is missing the following properties from type 'SocketIOClientStatic': protocol, Socket, Manager, managersts(2739)

All these properties are not mentioned as options in intellisense, or in he socketIO docs... they shouldn't be needed when creating an io()

How can I use Typescript with the SocketIO client?


回答1:


The io function has return type SocketIOClient.Socket and not SocketIOClientStatic. If you use that type, or leave out the signature altogether, it works as expected:

import io from 'socket.io-client';

const socket: SocketIOClient.Socket = io('http://localhost');
const anotherSocket = io('http://localhost');


来源:https://stackoverflow.com/questions/57189736/how-to-use-socket-io-client-type-definitions-for-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!