Skip to content

Instantly share code, notes, and snippets.

@Crusader4Christ
Last active April 22, 2024 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Crusader4Christ/625b012962ffe667141016f929978ca3 to your computer and use it in GitHub Desktop.
Save Crusader4Christ/625b012962ffe667141016f929978ca3 to your computer and use it in GitHub Desktop.
Typeguard method with enheritance
export abstract class BaseClass {
/**
* Generic method to typeguard provided instance
* @param entity
*/
public static classOf<T extends typeof BaseClass>(
this: T,
entity: BaseClass,
): entity is InstanceType<T> {
return entity.constructor.name === this.name;
}
/**
* Generic method to check type of entity
* @param baseClass
*/
public isA(baseClass: typeof BaseEntity): this is typeof baseClass {
return this.constructor.name === baseClass.name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment