Can someone help me find a dual approach?
- I am interested in customizing strings based on type.
- I want to be able to determine the type of a string different from a primitive string during runtime.
Take a look at this 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 aim for all three checks below to succeed.
I've also been experimenting with string casting, but I'm struggling to verify the variable's type during runtime.
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;
}