How to properly extend with typescript?
AngeloTadeucci opened this issue · comments
Ângelo Tadeucci commented
Example snippet:
function validEmail() {
return (email: string) => {
const re =
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
};
}
v8n.extend({
validEmail,
});
v8n().validEmail().test("angelo@email"); // error here on 'validEmail'
The extend functionality works fine but when building, for example with Next, it'll throw an error like:
Type error: Property 'validEmail' does not exist on type 'V8nValidator'.
Thanks! :D
Bruno C. Couto commented
HI @AngeloTadeucci, thank you for your question.
I don't know a pretty way of doing that other than "extending" the V8nValidator
interface.
I would go with something like:
declare module "v8n" {
export interface V8nValidator {
validEmail: () => V8nValidator;
}
}
I hope it can help you someway. 🙂
Ângelo Tadeucci commented
Thank you!