imbrn / v8n

☑️ JavaScript fluent validation library

Home Page:https://imbrn.github.io/v8n

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to properly extend with typescript?

AngeloTadeucci opened this issue · comments

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

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. 🙂

Thank you!