I am looking for a 1-2 punch.
- I'd like to typecast custom strings.
- Within runtime I'd like to be able to know the type of a string different from a primitive string.
Here's the code:
class TZDatabaseName extends String {
constructor(...args) {
super(...args);
return this;
}
}
expect(new TZDatabaseName('Asia/Tokyo') instanceof String).toBeTruthy();
expect(new TZDatabaseName('Asia/Tokyo') instanceof TZDatabaseName).toBeTruthy();
expect(new TZDatabaseName('Asia/Tokyo')).toEqual('Asia/Tokyo');
I would like all three of the checks below to pass.
I also have been messing with this method of casting strings as well but I have no way of checking in runtime the type of the variable.
export abstract class TZDatabaseName extends String {
public static MAKE(s: string): TZDatabaseName {
if (!s.match(/^\w+\/\w+$/)) throw new Error('invalid TZDatabaseName');
return s as any;
}
private __TZDatabaseNameFlag;
}