Math.abs for string literal type in TypeScript
21 Jun 2021
Today we discuss Absolute
To return a number here is a very complicated task because a number of iterations is limited (if we try to solve it with arrays). Instead we can use string literals and return a string as a result.
Here we go 🚀
Return result for the string
If we have a string, and we know that there's a number in it, we can check for a minus sign at the beginning:
Otherwise, we return the same string as we expect it to be positive
Return result for the number and bigint
Still we return the same number or bigint if we pass it to Absolute
As we already can manipulate numbers in a string, let's put a number to a string:
And now all the tests are passed ✅ – https://tsplay.dev/WJ9oRm
Good night ⛈🌙
typescript