NativeScript/NativeScript · error · TypeError

Callback must be a valid function.

Error message

Callback must be a valid function.

What it means

verifyCallback(value) is a helper that asserts an optional callback argument is actually a function when a truthy value is supplied. Passing a non-function truthy value (string, object, number) where a callback is expected throws this TypeError.

Source

Thrown at packages/core/utils/types.ts:43

	return typeof value === 'object';
}

export function isUndefined(value: any): value is undefined {
	return typeof value === 'undefined';
}

export function isDefined(value: any): boolean {
	return typeof value !== 'undefined';
}

export function isNullOrUndefined(value: any): boolean {
	return typeof value === 'undefined' || value === null;
}

export function verifyCallback(value: any) {
	if (value && !isFunction(value)) {
		throw new TypeError('Callback must be a valid function.');
	}
}

export function numberHasDecimals(value: number): boolean {
	return !(value % 1 === 0);
}

export function numberIs64Bit(value: number): boolean {
	return value < -Math.pow(2, 31) + 1 || value > Math.pow(2, 31) - 1;
}

const classInfosMap = new Map<Function, ClassInfo>();

// ES3-5 type classes are "function blah()", new ES6+ classes can be "class blah"
const funcNameRegex = /(?:function|class)\s+(\w+).*/;
export function getClass(object: Object): string {
	return getClassInfo(object).name;
}

View on GitHub (pinned to 6800aefa65)

Solutions

  1. Pass an actual function reference: `obj.on('event', this.onEvent.bind(this))` or an arrow function.
  2. If the callback is optional, pass `undefined`/`null` rather than a truthy placeholder.
  3. Fix typos like `obj.on('tap', handler())` (invoked, evaluates to non-function return value) to `obj.on('tap', handler)`.

Example fix

// before
button.on('tap', 'onTap'); // string, not function

// after
button.on('tap', (args) => this.onTap(args));
Defensive patterns

Strategy: type-guard

Validate before calling

if (callback != null && typeof callback !== 'function') {
  throw new TypeError('Callback must be a valid function.');
}

Type guard

function isCallback(v: unknown): v is (...args: any[]) => void {
  return typeof v === 'function';
}

Try / catch

try {
  verifyCallback(handler);
} catch (e) {
  if (e instanceof TypeError) { handler = () => {}; } else { throw e; }
}

Prevention

When it happens

Trigger: Calling any API that validates its arguments with verifyCallback (e.g. event addEventListener/subscribe-style APIs in core) passing a truthy non-function such as a handler name string, an options object, or an arrow function stored under the wrong key.

Common situations: Passing `'handleTap'` (method name) instead of the function reference; forgetting the argument and passing a config object in its place; refactors renaming handlers leaving stale references.

Related errors


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