I need to add my own method into a string in typescript. so hopefully I can do something like this
const myPoint : string = "23";
const customNumber = myPoint.convertUsingMyCustomImplementation();
I try this (using capital S) :
// eslint-disable-next-line no-extend-native
String.prototype.convertUsingMyCustomImplementation = function() : number {
return parseFloat(this);
};
but I have two errors
Property 'convertUsingMyCustomImplementation' does not exist on type 'String'.
Argument of type 'String' is not assignable to parameter of type 'string'. 'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.
and I also try the code below:
// eslint-disable-next-line no-extend-native
string.prototype.convertUsingMyCustomImplementation = function() : number {
return parseFloat(this);
};
but I also have an error
how to achieve that using Typescript?