Replace occurrence in string literal type in TypeScript
Created: 17 May 2021Updated: 17 May 2021
Today we discuss Replace
We need to remove one occurrence in the string and place instead the different value
Let's do that 🚀
Replace occurrence
Let's first find occurrence within a string and replace it with the different value:
We don't know start and end but know for sure that From is in the string S.
That's not the perfect solution though: https://tsplay.dev/WGnp9m
So when the input string S is empty, we want to have it as a result. Let's replace never with S:
Still we missed the case where we try to replace empty From: https://tsplay.dev/WYJabw
Here we don't need to do anything:
The solution is available here: https://tsplay.dev/wgrVMW
Have a wonderful evening ☕️
typescript