Jump to content

Typescript: interface function that returns different types

Go to solution Solved by colonel_mortis,

You can overload the types of functions in typescript. This isn't quite the same as overloads in other languages, because the implementation has to be the same for all the overloads, but it works for what you're trying to do.

export function propEq(path: string, value: any): (obj: Object) => boolean; // Overload for no obj
export function propEq(path: string, value: any, obj: Object): boolean; // Overload with obj
export function propEq(path: string, value: any, obj?: Object): boolean | ((obj: Object) => boolean) { // Actual implementation signature
    return obj
        ? R.equals(pick(path, obj), value)
        : (obj: any) => R.equals(pick(path, obj), value);
}

I don't think you can write this as an arrow function, but this notation is equivalent.

I am creating a function that takes a path, a value and optionally an object that will find the path in the object and return if it matches the value, my problem is i am having a hard time typing this.

 

import * as R from 'ramda';
export type PropEqCurry = (obj: any) => boolean
export type PropEqCall =  boolean
export type PropEq = PropEqCurry | PropEqCall

export const propEq = (path: string, value: any, obj?: Object): PropEq => (obj
  ? R.equals(pick(path, obj), value)
  : (obj: any) => R.equals(pick(path, obj), value));

 
however, this requires me to set the type when using it which I don't like

const checkName = propEq('name', 'jest') as PropEqCurry;

I would like to return type to be inferred from the arguments passed

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

You can overload the types of functions in typescript. This isn't quite the same as overloads in other languages, because the implementation has to be the same for all the overloads, but it works for what you're trying to do.

export function propEq(path: string, value: any): (obj: Object) => boolean; // Overload for no obj
export function propEq(path: string, value: any, obj: Object): boolean; // Overload with obj
export function propEq(path: string, value: any, obj?: Object): boolean | ((obj: Object) => boolean) { // Actual implementation signature
    return obj
        ? R.equals(pick(path, obj), value)
        : (obj: any) => R.equals(pick(path, obj), value);
}

I don't think you can write this as an arrow function, but this notation is equivalent.

HTTP/2 203

Link to post
Share on other sites

16 hours ago, colonel_mortis said:

You can overload the types of functions in typescript. This isn't quite the same as overloads in other languages, because the implementation has to be the same for all the overloads, but it works for what you're trying to do.


export function propEq(path: string, value: any): (obj: Object) => boolean; // Overload for no obj
export function propEq(path: string, value: any, obj: Object): boolean; // Overload with obj
export function propEq(path: string, value: any, obj?: Object): boolean | ((obj: Object) => boolean) { // Actual implementation signature
    return obj
        ? R.equals(pick(path, obj), value)
        : (obj: any) => R.equals(pick(path, obj), value);
}

I don't think you can write this as an arrow function, but this notation is equivalent.

Thanks will try this on Monday when back at work :)

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×