Last active
January 6, 2021 12:59
-
-
Save airglow923/c43671b2caab34754545eb34c462f8a5 to your computer and use it in GitHub Desktop.
Somewhat useless typescript code for generic but maybe useful in the future
This file contains hidden or 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
const typePattern = /\s(.*)\]/; | |
interface GenericTaggable { | |
[Symbol.toStringTag]: string; | |
} | |
interface DefaultConstructible<T> { | |
new (): T; | |
} | |
function createSignature(value: any) { | |
return '<' + typePattern.exec(Object.prototype.toString.call(value))![1].toLowerCase() + '>' | |
} | |
class Factory<T> { | |
constructor(private ctor: new () => T) {} | |
getInstance(): T { | |
return new this.ctor(); | |
} | |
} | |
function factory<T>(t: DefaultConstructible<T>): T { | |
return new t(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: