问题
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