05 / 18

What is typeof of a function?

Difficulty: 4/10
type checking, runtime reflection, javascript specifications

it returns 'function'

Scenario Questions

0-2 years experience

  1. 1Imagine you are building a simple custom event emitter. You want to allow users to register listeners, but you need to make sure they are actually passing a callable function before adding it to your registry. How would you write this validation check using typeof?
  2. 2We have a utility function that accepts either a static configuration object or a function that dynamically returns that configuration. How would you write the conditional logic to handle both cases safely at runtime?

2-5 years experience

  1. 1We have a legacy deep-cloning utility in our codebase. A developer noticed that functions nested inside objects are either being stripped out or causing runtime crashes because the cloner uses 'typeof obj === "object"' to recurse. How would you debug this and ensure functions are handled or preserved correctly?
  2. 2You are building a lightweight form validation library where rules can be defined as functions. A teammate suggests using 'typeof value === "function"' to detect these rules. What are the edge cases here? For instance, how does this check behave with async functions, generator functions, or class constructors, and how would you handle them?

5-8 years experience

  1. 1You are designing a dependency injection container for a frontend framework. You need to distinguish between ES6 class constructors (which must be invoked with 'new') and standard factory functions (which must be called directly). Since typeof returns 'function' for both, how would you design a robust runtime detection mechanism to instantiate them correctly?
  2. 2In a high-performance state serialization library we are writing, we need to traverse deeply nested state trees. Since JSON.stringify silently omits functions, we want to detect them using typeof and either serialize their source code or stub them out. How would you design this traversal to minimize CPU overhead when dealing with massive objects?

8+ years experience

  1. 1Your organization is migrating a massive, decade-old JavaScript codebase to strict TypeScript. We have thousands of legacy runtime type-checks relying on typeof. How would you design a migration strategy and tooling pipeline to safely phase out these fragile runtime checks in favor of compile-time type safety without breaking legacy runtime behaviors?
  2. 2You are architecting an extensible micro-frontend platform where untrusted third-party plugins register lifecycle hooks. Some plugins pass standard functions, while others pass ES6 Proxies wrapping functions. How would you design a secure runtime boundary that validates these inputs, keeping in mind the limitations of typeof when dealing with Proxies and cross-realm execution?

Follow-up Questions

  • How does typeof behave when checking an ES6 class constructor versus a standard function?
  • What does typeof return if we pass a Proxy that wraps a target function?
  • If we are dealing with functions passed across different iframe boundaries, does typeof still behave reliably?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.