I'm one of those people moving from JavaScript to TypeScript.
While converting my code, i stumpled across my function in JS to replace a number at a given index, not being able to fully convert.
String.prototype.replaceAt = function (index, replacement) {
return this.slice(0, index) + replacement + this.slice(index + 1);
};
//Property 'replaceAt' does not exist on type 'String'. Did you mean 'replace'?ts(2551)
let string = "103456"
//"103456"
if (string.charAt(1) === "0") {
let newString = string.replaceAt(1,2) //replaces at Index(1) 0 to become 2
//Wanted outcome: "123456"
}
What this code does in JS
- Checks if the string has "0" as the second value (charAt(1))
- If true: replace the given number with 2 (...replaceAt(0,2))
- let newString become = "123456"
tl;dr check if match, if match = replace the value.
What I've Tried
As I'm still learning, I've checked up on declaring Globals, and interface StringConstructor. However, I'm unable to achieve the wanted outcome.