news.ycombinator.com• Feb 5, 2021• 1 min read
Strange TypeScript BehaviorAt Codesphere, we code mostly in Typescript, not necessarily because it is our favorite language, but because we found out that it made us the most productive.However, recently I came across two very strange behaviors, (I know, they are common in the JavaScript Bubble) and I felt the urge to share them!1: ['1', '2', '10'].map(parseInt); I came across this when I wanted to format some user input, convert it into numbers and put them in a chart.Alt Text['1', '2', '10'].map(parseInt);This does not work, because map passes three arguments into parseInt() on each iteration. The second argument index is passed into parseInt as a radix parameter. So each string in the array is parsed using a different radix. '2' is parsed as radix 1, which results in NaN, '10' is parsed as radix 2, which is 3, and '1' is parsed as the default radix 10 because its index 0 is falsy.2: Inheritance of 'readonly' in Typescript During a code review we came across the idea to make methods readonly. What happened nex