also looking at this
fix(inference): read every spelling of a number literal
#3121
3 files
+141
-40
| @@ -5,7 +5,7 @@ | ||
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | import type { Declaration } from '../types' |
| 8 | import { inferFunctionBodyReturnType, inferNarrowType } from '../processor/type-inference' | |
| 8 | import { inferFunctionBodyReturnType, inferNarrowType, isNumericLiteral } from '../processor/type-inference' | |
| 9 | 9 | |
| 10 | 10 | // Character codes for fast comparison |
| 11 | 11 | const CH_SPACE = 32 |
| @@ -1897,22 +1897,6 @@ function scanDeclarationsInternal(_source: string, _filename: string, _keepComme | ||
| 1897 | 1897 | return `${name}${optionalMarker}: ${type}` |
| 1898 | 1898 | } |
| 1899 | 1899 | |
| 1900 | /** Check if string is a numeric literal (integer or decimal, optionally negative) */ | |
| 1901 | function isNumericLiteral(v: string): boolean { | |
| 1902 | let i = 0 | |
| 1903 | if (i < v.length && v.charCodeAt(i) === 45 /* - */) i++ | |
| 1904 | if (i >= v.length) return false | |
| 1905 | const c = v.charCodeAt(i) | |
| 1906 | if (c < 48 || c > 57) return false // not a digit | |
| 1907 | while (i < v.length && v.charCodeAt(i) >= 48 && v.charCodeAt(i) <= 57) i++ | |
| 1908 | if (i < v.length && v.charCodeAt(i) === 46 /* . */) { | |
| 1909 | i++ | |
| 1910 | if (i >= v.length || v.charCodeAt(i) < 48 || v.charCodeAt(i) > 57) return false | |
| 1911 | while (i < v.length && v.charCodeAt(i) >= 48 && v.charCodeAt(i) <= 57) i++ | |
| 1912 | } | |
| 1913 | return i === v.length | |
| 1914 | } | |
| 1915 | ||
| 1916 | 1900 | /** Infer type from a default value expression (simple cases) */ |
| 1917 | 1901 | function inferTypeFromDefault(value: string): string { |
| 1918 | 1902 | const v = value.trim() |