NativeScript/NativeScript · error · Error

Value should not be negative, NaN or Infinity: ${value}

Error message

Value should not be negative, NaN or Infinity: ${value}

What it means

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.

Source

Thrown at packages/core/ui/layouts/grid-layout/grid-layout-common.ts:95

}

export class ItemSpec extends Observable implements ItemSpecDefinition {
	private _value: number;
	private _unitType: GridUnitType;
	toJSON?: () => any;

	constructor(...args) {
		super();

		if (args.length === 0) {
			this._value = 1;
			this._unitType = GridUnitType.STAR;
		} else if (arguments.length === 2) {
			const value = args[0];
			const type = args[1];
			if (typeof value === 'number' && typeof type === 'string') {
				if (value < 0 || isNaN(value) || !isFinite(value)) {
					throw new Error(`Value should not be negative, NaN or Infinity: ${value}`);
				}

				this._value = value;
				this._unitType = GridUnitType.parse(type);
			} else {
				throw new Error('First argument should be number, second argument should be string.');
			}
		} else {
			throw new Error('ItemSpec expects 0 or 2 arguments');
		}

		this.index = -1;
	}

	public owner: GridLayoutBase;
	public index: number;
	public _actualLength = 0;

View on GitHub (pinned to 6800aefa65)

Solutions

  1. Clamp or sanitize the value before constructing: Math.max(0, value) and check Number.isFinite(value).
  2. Fix the upstream computation that produced NaN/Infinity.
  3. Fall back to a sane default (e.g. new ItemSpec(1, 'auto')) when the computed value is invalid.

Example fix

// before
const starCount = total / perRow; // may be NaN
spec = new ItemSpec(starCount, 'star');
// after
const starCount = total / perRow;
spec = new ItemSpec(Number.isFinite(starCount) && starCount >= 0 ? starCount : 1, 'star');
Defensive patterns

Strategy: validation

Validate before calling

function safeItemSpec(value: number, type: string): ItemSpec {
  if (typeof value !== 'number' || !Number.isFinite(value) || value < 0) {
    value = 1;
  }
  return new ItemSpec(value, type);
}

Type guard

const isValidSpecValue = (v: unknown): v is number => typeof v === 'number' && Number.isFinite(v) && v >= 0;

Try / catch

try {
  spec = new ItemSpec(count, 'star');
} catch (e) {
  if (e.message.startsWith('Value should not be negative')) {
    spec = new ItemSpec(1, 'star');
  } else { throw e; }
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of NativeScript/NativeScript@6800aefa65 (2026-08-30). Data as JSON: /api/errors/c10c22c2d4772861. Report an issue: GitHub.