Omit fn proto when extending () => any" in TypeScript

I’m attempting to create a TypeScript API that returns a function with additional properties. This function also needs to have its Function.prototype properties removed. I have tried various combinations of Omit, Pick, etc., but haven’t been able to achieve the desired result.

My code so far is shown below:

type MainFunc = () => PublicInterface
type PublicFunction = () => PublicInterface

interface PublicInterface extends MainFunc {
  mainFunc: MainFunc
  anotherFunc: PublicFunction
}

const removeProto = (func: MainFunc) => {
  Object.setPrototypeOf(func, null)
  return func
}

const mainFunc: MainFunc = (): PublicInterface => createPublicInterface()
const anotherFunc: PublicFunction = () => createPublicInterface()

const createPublicInterface = (): PublicInterface =>
  Object.assign(
    removeProto(mainFunc),
    {
      mainFunc,
      anotherFunc
    }
  )

How can I create a type that is a function and correctly omits Function.prototype properties?

You can create a type that omits Function.prototype properties using the Omit utility type in TypeScript. Here’s an updated version of your code that achieves this:

type MainFunc = () => PublicInterface
type PublicFunction = () => PublicInterface

interface PublicInterface extends Omit<MainFunc, keyof Function> {
  mainFunc: MainFunc
  anotherFunc: PublicFunction
}

const removeProto = (func: MainFunc) => {
  Object.setPrototypeOf(func, null)
  return func
}

const mainFunc: MainFunc = (): PublicInterface => createPublicInterface()
const anotherFunc: PublicFunction = () => createPublicInterface()

const createPublicInterface = (): PublicInterface =>
  Object.assign(
    removeProto(mainFunc),
    {
      mainFunc,
      anotherFunc
    }
  )

In this updated code, the PublicInterface interface extends MainFunc with the Omit utility type, which omits all properties from Function (including Function.prototype).