DEV Community

Cover image for Function overload in TypeScript
Ramu Narasinga
Ramu Narasinga

Posted on • Updated on

Function overload in TypeScript

When working with TypeScript, you may encounter situations where a function needs to handle different types of input while maintaining type safety. This is where function overloading comes into play. Let’s look at a practical example of function overloading, inspired by a code snippet from the Supabase source code.

Example: useIsFeatureEnabled

The useIsFeatureEnabled function is a great example of function overloading. It can handle both an array of features and a single feature, returning appropriate results for each case.

Here’s the overloaded function definition:

function useIsFeatureEnabled<T extends Feature\[\]>(
  features: T
): { \[key in FeatureToCamelCase<T\[number\]>\]: boolean }
function useIsFeatureEnabled(features: Feature): boolean
function useIsFeatureEnabled<T extends Feature | Feature\[\]>(features: T) {
  const { profile } = useProfile()

  if (Array.isArray(features)) {
      return Object.fromEntries(
        features.map((feature) => \[
          featureToCamelCase(feature),
          checkFeature(feature, profile?.disabled\_features),
        \])
      )
  }
  return checkFeature(features, profile?.disabled\_features)
}
export { useIsFeatureEnabled }
Enter fullscreen mode Exit fullscreen mode

How It Works

  1. Function Overloads: The first two declarations are overload signatures. They define the different ways the function can be called. The actual implementation comes last, handling both cases.
  2. Implementation: The function implementation checks if the input features is an array. If it is, it processes each feature, converts it to camelCase, and checks if it's enabled. If features is a single feature, it directly checks its status.

Supporting Functions and Types

To understand this better, let’s look at the supporting checkFeature function and the type utility FeatureToCamelCase.

checkFeature Function

The checkFeature function determines if a given feature is enabled or not:

function checkFeature(feature: Feature, features?: Feature\[\]) {
  return !features?.includes(feature) ?? true
}
Enter fullscreen mode Exit fullscreen mode

This function returns true if the feature is not in the disabled features list or if no disabled features are provided.

Do watch the Matt Pocock’s Youtube video explanation about the function overloads in TypeScript.

Conclusion

Function overloading in TypeScript allows you to define multiple ways to call a function with different types of input while ensuring type safety. The useIsFeatureEnabled function from Supabase is an excellent example of this concept in action. It demonstrates how to handle different input types seamlessly, providing both flexibility and strong typing.

Get free courses inspired by the best practices used in open source.

About me:

Website: https://ramunarasinga.com/

Linkedin: https://www.linkedin.com/in/ramu-narasinga-189361128/

Github: https://github.com/Ramu-Narasinga

Email: ramu.narasinga@gmail.com

Learn the best practices used in open source.

References:

  1. https://github.com/supabase/supabase/blob/master/apps/studio/hooks/misc/useIsFeatureEnabled.ts#L24
  2. Matt Pocock’s Youtube video explanation.

Top comments (0)