当尝试从this module扩展Adapter类以用于TS项目时,我遇到了问题。
声明模块,
function memberships_user_membership_saved( $param_1, $param_2 ) {
$to = 'mail@test.com';
$subject = 'The subject';
$body = '<pre>', print_r($param_1, 1), '</pre>';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
}
add_action( 'wc_memberships_user_membership_saved', 'memberships_user_membership_saved', 10, 2 );
扩展
declare module 'socket.io-adapter' {
import * as Events from 'events';
import * as SocketIO from 'socket.io';
export default class Adapter extends Events.EventEmitter {
constructor(nsp: SocketIO.Namespace);
nsp: SocketIO.Namespace;
rooms: SocketIO.Rooms
sids: { [id: string]: { [room: string]: boolean } };
encoder: any;
public add(id: string, room: string, fn?: (err?: any) => void): void;
public addAll(id: string, rooms: string[], fn?: (err?: any) => void): void;
public del(id: string, room: string, fn?: (err?: any) => void): void;
public delAll(id: string, rooms: string[], fn?: () => void): void;
public broadcast(packet: any, opts: any): void;
public clients(rooms: string[], fn: (this: this, error: null, sids: string[]) => void): void;
public clientRooms(id: string, fn: (this: this, error: null, rooms: string[] | null) => void): void;
}
class Room {
sockets: { [key: string]: boolean };
length: number;
add(id: string): void;
del(id: string): void;
}
}
并尝试使用它
import Adapter from 'socket.io-adapter';
export default class LocalAdapter extends Adapter {}
结果
import SocketIO from 'socket.io';
import LocalAdapter from './LocalAdapter';
...
io = SocketIO(server, { adapter: LocalAdapter });
(alias) SocketIO(): SocketIO.Server (+3 overloads)
import SocketIO
Default Server constructor
好像类没有属性...
文档指出SocketIO.ServerOptions.adapter必须是适配器的类型,它引用的是类而不是实例对象。
我阅读了《定型指南》,但在这种情况下(模块默认情况下会导出一个类),我看不到实现它的方法。