Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I let TS compiler know a function exist in an object?

How do I let TS compiler know a method is in an object?

I have a function, purpose of that is check a function is in an object, my expecting is if hasFunction returns true, that means function exist in object that I’m checking, and typescript compiler stop giving me error:

function hasFunction(obj: any, func: string) {
    if (func in obj && (typeof obj[func] === "function"))
        return true;
    return false;
}

I tried to use it, but received an error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function callMyFunc(obj: object) {
    if (hasFunction(obj, "myFunc")) {
        obj.myFunc();
    }
}
// error: Property 'myFunc' does not exist on type 'object'.

I tried something like that, but ‘in’ is not a valid syntax:

function hasFunction(obj: any, func: string): func in obj

so what can I do to let compiler know that function exist on object?

>Solution :

Your function needs to be generic over func. You can store its literal type in a generic type K. Now we only need a type predicate, so that the caller knows that obj has a key K which holds a function.

For the function type, you have to choose the right type for your needs. I went with the "unsafe" any approach. In the type guard, you can’t really check parameter and return types of the function because they don’t exist at runtime. So you might as well just type them as unknown.

function hasFunction<K extends string>(obj: any, func: K): obj is { 
  [Key in K]: (...args: any) => any 
} {
    return func in obj && (typeof obj[func] === "function")
}

Playground

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading