Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer return type of a function with provided argument types #26043

Closed
int0h opened this issue Jul 28, 2018 · 5 comments
Closed

Infer return type of a function with provided argument types #26043

int0h opened this issue Jul 28, 2018 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@int0h
Copy link

int0h commented Jul 28, 2018

I'm trying to create something similar to native ReturnType type. In comparison to the later, it should also consider generic types of arguments.

I guess the idea will be clear from the code below.

If it can be done now - I'd be happy to receive some links. Otherways it'd be great to know if there are any plans to cover that behavior. In case it could be achieved by another not-implemented feature I'll be glad for the link to an issue to be able to track its progress.

TypeScript Version: 2.9.2

Search Terms: infer function return type

Code

// it works with actual code:
function identity<T>(a: T) {
    return a;
}

const one = identity(1); // typeof one === 1 # everything is great

function call<A, R>(arg: A, fn: Func<A, R>): R {
    return fn(arg);
}

const two = call(2 as 2, i => i) // typeof two === 2 # everything is great

// but won't work with types
type Func<A, R> = (arg: A) => R;
// ReturnTypeByArg is inspired by `ReturnType` from TS 2.8.*
type ReturnTypeByArg<T, F extends Func<T, any>> = F extends Func<T, infer R> ? R : any;

type Identity = <T>(a: T) => T;
type Result = ReturnTypeByArg<4, Identity>; // Result === {} # not what I expect

Expected behavior:
I expect TS to be able to infer such return type

Actual behavior:
Return type is {}

Playground Link: link

Related Issues:

@mhegazy
Copy link
Contributor

mhegazy commented Jul 30, 2018

Looks like a duplicate of #6606

@mhegazy mhegazy added the Duplicate An existing issue was already created label Jul 30, 2018
@int0h
Copy link
Author

int0h commented Jul 31, 2018

@mhegazy I guess #6606 is a much more complex feature request. Mine looks like only one case of "Call" type which was referenced by @treybrisbane. And if such Call type is implemented it will cover my issue also.

But I thought my problem can be solved through new 2.8.* infer key-word. So I see it rather as a bug than a feature request.

It would be great if you provided me with a link to an issue about such Call type if one exists.

By the way, since #6606 is closed I'm not sure what do you mean by marking mine as a duplicate. Does it mean this issue won't be fixed?

@mhegazy
Copy link
Contributor

mhegazy commented Jul 31, 2018

The underlying scenario is the same. #6606 proposed using typeof as syntax, but you can see the same happening using a call syntax foo(number, string) without the typeof keyword.

The complexity in implementing either is applying overload resolution as a higher-order type operator. the overload resolution is extremely complicated process, that involved multiple passes and required multiple processes like contextual types, inference, and instantiations to interact. Enabling this, at least in the current compiler implementation, for higher order types is tantamount to rewriting the core of the compiler; something we are not looking forward to.

You can seem my reply in #6606 (comment) as well.

@malash
Copy link

malash commented Dec 14, 2018

I believe that's not a bug.

infer R is the union of all available types.

const f1: Func<4, 4> = identity;
// @ts-ignore f2 failed
const f2: Func<4, 1> = identity;
const f3: Func<4, number> = identity;
const f4: Func<4, any> = identity;
const f5: Func<4, {}> = identity;

As you can see the second argument of Func could be any, as known as {}.

So infer R should be union of them and it's {}.

For a given infer type variable V, if any candidates were inferred from co-variant positions, the type inferred for V is a union of those candidates. https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html

@int0h

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants