Ionic2: Extending string functions


I would like to extend the string functions. Therefore i created the following structure:
- extensions

My string.ts files contains the following

String.prototype.isValidEmail = function() {
    var 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(this);

interface String {
    isValidEmail(): boolean;

If i now use this function i get this error : is not a function, most likely because does not include it in the build process. My question is where should i include this, so this function would be recognized?


I would like to know to implement this in a Angular/Ionic way too. Did you find a solution?

See this SO topic.

Any solution??

I’am dealing with the same problem :frowning:


In Angular I include these kinds of things in my polyfill.ts file, but in Ionic this doesn’t seem to be available for editing (polyfills.js is created in the build process from somewhere inaccessible). For now I’m including them in main.ts, but it doesn’t really feel quite right.