{"record":{"id":"c10c22c2d4772861","repo":"NativeScript/NativeScript","slug":"value-should-not-be-negative-nan-or-infinity-v","errorCode":null,"errorMessage":"Value should not be negative, NaN or Infinity: ${value}","messagePattern":"Value should not be negative, NaN or Infinity: (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/ui/layouts/grid-layout/grid-layout-common.ts","lineNumber":95,"sourceCode":"}\n\nexport class ItemSpec extends Observable implements ItemSpecDefinition {\n\tprivate _value: number;\n\tprivate _unitType: GridUnitType;\n\ttoJSON?: () => any;\n\n\tconstructor(...args) {\n\t\tsuper();\n\n\t\tif (args.length === 0) {\n\t\t\tthis._value = 1;\n\t\t\tthis._unitType = GridUnitType.STAR;\n\t\t} else if (arguments.length === 2) {\n\t\t\tconst value = args[0];\n\t\t\tconst type = args[1];\n\t\t\tif (typeof value === 'number' && typeof type === 'string') {\n\t\t\t\tif (value < 0 || isNaN(value) || !isFinite(value)) {\n\t\t\t\t\tthrow new Error(`Value should not be negative, NaN or Infinity: ${value}`);\n\t\t\t\t}\n\n\t\t\t\tthis._value = value;\n\t\t\t\tthis._unitType = GridUnitType.parse(type);\n\t\t\t} else {\n\t\t\t\tthrow new Error('First argument should be number, second argument should be string.');\n\t\t\t}\n\t\t} else {\n\t\t\tthrow new Error('ItemSpec expects 0 or 2 arguments');\n\t\t}\n\n\t\tthis.index = -1;\n\t}\n\n\tpublic owner: GridLayoutBase;\n\tpublic index: number;\n\tpublic _actualLength = 0;\n","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/NativeScript/NativeScript/blob/6800aefa6511d23ddad8beb38ccc8541c5e91628/packages/core/ui/layouts/grid-layout/grid-layout-common.ts#L77-L113","documentation":"The ItemSpec constructor, when called with (value: number, type: string), validates that the value is a finite non-negative number before creating the track definition. Negative counts, NaN, or Infinity are meaningless for grid track sizing, so the constructor throws immediately.","triggerScenarios":"new ItemSpec(-1, 'pixel'), new ItemSpec(NaN, 'auto'), new ItemSpec(Infinity, 'star'), or passing a computed value that evaluated to NaN/Infinity (e.g. from a division by zero or parseInt of a bad string).","commonSituations":"Computing star counts from dynamic data where the math went wrong; hard-coding negative sizes by mistake; feeding user input straight into ItemSpec without sanitization.","solutions":["Clamp or sanitize the value before constructing: Math.max(0, value) and check Number.isFinite(value).","Fix the upstream computation that produced NaN/Infinity.","Fall back to a sane default (e.g. new ItemSpec(1, 'auto')) when the computed value is invalid."],"exampleFix":"// before\nconst starCount = total / perRow; // may be NaN\nspec = new ItemSpec(starCount, 'star');\n// after\nconst starCount = total / perRow;\nspec = new ItemSpec(Number.isFinite(starCount) && starCount >= 0 ? starCount : 1, 'star');","handlingStrategy":"validation","validationCode":"function safeItemSpec(value: number, type: string): ItemSpec {\n  if (typeof value !== 'number' || !Number.isFinite(value) || value < 0) {\n    value = 1;\n  }\n  return new ItemSpec(value, type);\n}","typeGuard":"const isValidSpecValue = (v: unknown): v is number => typeof v === 'number' && Number.isFinite(v) && v >= 0;","tryCatchPattern":"try {\n  spec = new ItemSpec(count, 'star');\n} catch (e) {\n  if (e.message.startsWith('Value should not be negative')) {\n    spec = new ItemSpec(1, 'star');\n  } else { throw e; }\n}","preventionTips":["Sanitize computed values with Number.isFinite and a >= 0 check before constructing ItemSpec.","Never pass raw user input or unsanitized JSON numbers as ItemSpec values.","Default to 1 (single star/auto unit) when a computed size is invalid."],"tags":["gridlayout","itemspec","numeric-validation","constructor"],"backgroundTag":"invalid-numeric-argument","analyzedSha":"6800aefa6511d23ddad8beb38ccc8541c5e91628","analyzedAt":"2026-08-30T20:04:56.809Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}