23 lines
1.2 KiB
TypeScript
23 lines
1.2 KiB
TypeScript
import type { MessageType } from "./message-type.js";
|
|
import type { AnyMessage } from "./message.js";
|
|
import { Message } from "./message.js";
|
|
/**
|
|
* Check whether the given object is any subtype of Message or is a specific
|
|
* Message by passing the type.
|
|
*
|
|
* Just like `instanceof`, `isMessage` narrows the type. The advantage of
|
|
* `isMessage` is that it compares identity by the message type name, not by
|
|
* class identity. This makes it robust against the dual package hazard and
|
|
* similar situations, where the same message is duplicated.
|
|
*
|
|
* This function is _mostly_ equivalent to the `instanceof` operator. For
|
|
* example, `isMessage(foo, MyMessage)` is the same as `foo instanceof MyMessage`,
|
|
* and `isMessage(foo)` is the same as `foo instanceof Message`. In most cases,
|
|
* `isMessage` should be preferred over `instanceof`.
|
|
*
|
|
* However, due to the fact that `isMessage` does not use class identity, there
|
|
* are subtle differences between this function and `instanceof`. Notably,
|
|
* calling `isMessage` on an explicit type of Message will return false.
|
|
*/
|
|
export declare function isMessage<T extends Message<T> = AnyMessage>(arg: unknown, type?: MessageType<T>): arg is T;
|