{"record":{"id":"26123f35baf1acc0","repo":"NativeScript/NativeScript","slug":"callback-must-be-a-valid-function","errorCode":null,"errorMessage":"Callback must be a valid function.","messagePattern":"Callback must be a valid function\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/core/utils/types.ts","lineNumber":43,"sourceCode":"\n\treturn typeof value === 'object';\n}\n\nexport function isUndefined(value: any): value is undefined {\n\treturn typeof value === 'undefined';\n}\n\nexport function isDefined(value: any): boolean {\n\treturn typeof value !== 'undefined';\n}\n\nexport function isNullOrUndefined(value: any): boolean {\n\treturn typeof value === 'undefined' || value === null;\n}\n\nexport function verifyCallback(value: any) {\n\tif (value && !isFunction(value)) {\n\t\tthrow new TypeError('Callback must be a valid function.');\n\t}\n}\n\nexport function numberHasDecimals(value: number): boolean {\n\treturn !(value % 1 === 0);\n}\n\nexport function numberIs64Bit(value: number): boolean {\n\treturn value < -Math.pow(2, 31) + 1 || value > Math.pow(2, 31) - 1;\n}\n\nconst classInfosMap = new Map<Function, ClassInfo>();\n\n// ES3-5 type classes are \"function blah()\", new ES6+ classes can be \"class blah\"\nconst funcNameRegex = /(?:function|class)\\s+(\\w+).*/;\nexport function getClass(object: Object): string {\n\treturn getClassInfo(object).name;\n}","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/NativeScript/NativeScript/blob/6800aefa6511d23ddad8beb38ccc8541c5e91628/packages/core/utils/types.ts#L25-L61","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass an actual function reference: `obj.on('event', this.onEvent.bind(this))` or an arrow function.","If the callback is optional, pass `undefined`/`null` rather than a truthy placeholder.","Fix typos like `obj.on('tap', handler())` (invoked, evaluates to non-function return value) to `obj.on('tap', handler)`."],"exampleFix":"// before\nbutton.on('tap', 'onTap'); // string, not function\n\n// after\nbutton.on('tap', (args) => this.onTap(args));","handlingStrategy":"type-guard","validationCode":"if (callback != null && typeof callback !== 'function') {\n  throw new TypeError('Callback must be a valid function.');\n}","typeGuard":"function isCallback(v: unknown): v is (...args: any[]) => void {\n  return typeof v === 'function';\n}","tryCatchPattern":"try {\n  verifyCallback(handler);\n} catch (e) {\n  if (e instanceof TypeError) { handler = () => {}; } else { throw e; }\n}","preventionTips":["Pass function references, not method-name strings.","Don't invoke the function when passing it (no handler() as argument).","For optional callbacks pass undefined/null, not truthy placeholders."],"tags":["validation","callback","typeerror"],"backgroundTag":"invalid-callback","analyzedSha":"6800aefa6511d23ddad8beb38ccc8541c5e91628","analyzedAt":"2026-08-30T20:04:56.809Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}