import { Constructor } from '../typings/types'; export interface IsInstance { (declaration: Constructor, target: unknown, loose?: boolean): target is T; (declaration: Constructor): (target: unknown, loose?: boolean) => target is T; } /** * Native `instanceof` checks are problematic, as cross realm checks fail. * They will also fail when comparing against source and built files. * So emulate an `instanceof` check by comparing constructor names. */ /** * Performs a loose instance check by comparing class names up the prototype * chain if `instanceof` initially fails. To disable this loose check, * pass `false` as the 3rd argument. * * ```ts * import { isInstance } from 'tily/is/instance'; * * if (isInstance(Error, error)) { * console.log(error.stack); * } * ``` * * Generics can be used to type the object being checked. This will default * to the declaration passed to the 2nd argument. * * ```ts * isInstance(Error, error); * ``` * * > Loose checks can be useful if multiple copies of the same class declaration * > exists in the module tree. For example, multiple versions of the same package are imported. * * @param target The object to check * @param declaration The constructor * @param loose Whether use loose checks. Default is true. * * @example * * isInstance(Foo, undefined); //=> false * isInstance(Foo, null); //=> false * * class Foo {} * class Bar {} * * isInstance(Foo, new Foo()); //=> true * isInstance(Foo, new Bar()); //=> false * * class Baz {} * Object.defineProperty(Baz, 'name', { value: 'Foo' }); * * isInstance(Foo, new Baz()); //=> true * isInstance(Foo, new Baz(), false); //=> false * * const isFoo = isInstance(Foo); * * isFoo(new Baz()); //=> true * isFoo(new Baz(), false); //=> false * */ export declare const isInstance: IsInstance; export default isInstance;