NativeScript/NativeScript · error · TypeError

Callback, if provided, must be function.

Error message

Callback, if provided, must be function.

What it means

The static Observable.removeEventListener throws this TypeError when a truthy `callback` argument is provided that is not a function. Like the instance variant, it validates types before touching the global handler registry.

Source

Thrown at packages/core/data/observable/index.ts:341

			entries.splice(i, 1);
			i--;
		}
	}

	/**
	 * Please avoid using the static event-handling APIs as they will be removed
	 * in future.
	 * @deprecated
	 */
	public static removeEventListener(eventName: string, callback?: (data: EventData) => void, thisArg?: any): void {
		thisArg = thisArg || undefined;

		if (typeof eventName !== 'string') {
			throw new TypeError('Event name must be a string.');
		}

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

		const eventClass = this.name === 'Observable' ? '*' : this.name;

		const entries = _globalEventHandlers?.[eventClass]?.[eventName];
		if (!entries) {
			return;
		}

		const countBeforeRemoval = entries.length;
		Observable.innerRemoveEventListener(entries, callback, thisArg);
		_globalEventHandlersCount -= countBeforeRemoval - entries.length;

		if (!entries.length) {
			// Clear all entries of this type
			delete _globalEventHandlers[eventClass][eventName];
		}

View on GitHub (pinned to 6800aefa65)

Solutions

  1. Pass the original function reference used with static addEventListener
  2. If no callback is needed, omit the second argument entirely
  3. Add a `typeof cb === 'function'` check before calling

Example fix

// before
Observable.off('loaded', this.handlers['loaded']);
// after
const cb = this.handlers['loaded'];
if (typeof cb === 'function') Observable.off('loaded', cb);
Defensive patterns

Strategy: type-guard

Validate before calling

if (callback === undefined || typeof callback === 'function') Observable.removeEventListener(eventName, callback);

Type guard

const isHandler = (v: unknown): v is (data: import('@nativescript/core').EventData) => void => typeof v === 'function';

Try / catch

try { Observable.off(name, cb); } catch (e) { if (e instanceof TypeError) { /* drop bad handler ref, resubscribe */ } else throw e; }

Prevention

When it happens

Trigger: Calling `Observable.off('eventName', someObject)` or the deprecated static removeEventListener with a non-function second argument while unsubscribing from class-level (global) events.

Common situations: Passing an event emitter, promise, or config object where the handler belongs; stale imports where the handler name refers to a non-callable export.

Related errors


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