Replace all occurrences in string literal type in TypeScript
22 May 2021
Today we discuss ReplaceAll
We need to remove all occurrences in the string and place instead the different value
Let's start ⏳
Replace all occurrences
Let's check the solution for Replace and adapt it for ReplaceAll
:
This solution substitutes From
for To
once: https://tsplay.dev/mA7ZXw
Let's do that as many times as required:
But if we call ReplaceAll
recursively this way, we will do it incorrectly. Let me give an example:
So instead of calling ReplaceAll
for the whole string, let's do it for the rest of the string, or End
:
Thank you for your time! ⌛️
The final solution with test cases are available here: https://tsplay.dev/mplJBm
Have a wonderful evening 👩💻
typescript