I am currently developing an API in typescript where I aim to return a function that includes multiple functions as properties, one of which is the same function.
My approach to achieving this includes:
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
}
)
Within the Javascript code, I have also taken steps to remove the prototype of MainFunc, such as 'apply' and 'call', as I do not wish to utilize them. However, I am struggling to figure out how to remove it from the type as well.
I have attempted various combinations of Omit, Pick, and so forth, but have not had any success thus far.
Edit:
I have included additional code above to provide further context. Although the actual code is more intricate, I hope this helps illustrate the issue at hand.
In essence, I have a public interface on a function. In the actual code, the 'MainFunction' varies based on context, but it always returns another function with a public interface for the purpose of chaining.
I manually remove the prototype from the main function, and then assign the public interface to it.
While everything operates smoothly in JS alone, the types function appropriately if the prototype is not removed. Unfortunately, I have been unable to effectively communicate to Typescript that a function has custom properties, making it challenging to provide accurate intellisense.
My Progress So Far Through Experimentation:
I have a type that includes my PublicInterface, but does not acknowledge that it is also a function itself.
I have a type that recognizes it as a function and includes my public interface, however, all the Function.prototype properties (such as 'apply' and 'call') are set to 'never'. Despite this, they still appear in intellisense, creating a subpar user experience. https://i.sstatic.net/pB3vE.png
If individuals are considering closing the question, could you kindly provide an explanation so that I may address it, instead of simply making that decision without clarification?