-
Notifications
You must be signed in to change notification settings - Fork 0
/
type-guards.ts
64 lines (57 loc) · 1.69 KB
/
type-guards.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { Notification } from "./notification.ts";
import { Request } from "./request.ts";
import {
Constructor,
Handler,
Message,
NotificationConstructor,
NotificationHandler,
RequestConstructor,
RequestHandler,
} from "./types.ts";
function isNotification<TNotification extends Notification>(
request: TNotification,
): request is TNotification & { constructor: NotificationConstructor } {
return isNotificationConstructor(request.constructor as Constructor<Message>);
}
function isNotificationHandler(
handler: Handler<Message>,
): handler is NotificationHandler<Notification> {
return typeof handler === "function";
}
function isNotificationConstructor(
constructor: Constructor<Message>,
): constructor is NotificationConstructor<Notification> {
return (
"notificationTypeId" in constructor &&
constructor.notificationTypeId != null &&
typeof constructor.notificationTypeId === "symbol"
);
}
function isRequest<TRequest extends Request = Request>(
request: TRequest,
): request is TRequest & { constructor: RequestConstructor<Request> } {
return isRequestConstructor(request.constructor as Constructor<Message>);
}
function isRequestConstructor(
constructor: Constructor<Message>,
): constructor is RequestConstructor<Request> {
return (
"requestTypeId" in constructor &&
constructor.requestTypeId != null &&
typeof constructor.requestTypeId === "symbol"
);
}
function isRequestHandler(
handler: Handler<Message>,
): handler is RequestHandler<Request> {
return typeof handler === "function";
}
export const TypeGuards = {
isRequestConstructor,
isRequestHandler,
isRequest,
isNotification,
isNotificationHandler,
isNotificationConstructor,
};