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.

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 accountSign in
Already have an account? Sign in here.
Sign In Now