Last active
April 22, 2024 09:27
-
-
Save Crusader4Christ/625b012962ffe667141016f929978ca3 to your computer and use it in GitHub Desktop.
Typeguard method with enheritance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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