While working on my Angular 12 application, I developed the following string extension:
declare global {
interface String {
toNumber(): number | null;
}
}
Object.defineProperty(String.prototype, "toNumber", {
value: function(this: string) {
return Number(this) || null;
}
});
However, when attempting to use this extension in an Angular component:
var number = stringValue.toNumber();
An error is thrown:
Property 'toNumber' does not exist on type 'string'.
I'm seeking advice on how best to utilize extensions like this in Angular. Should I modify the way I create the extension?