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

Type inference resulting in incorrect type for function result #14798

Closed
magnushiie opened this issue Mar 22, 2017 · 5 comments
Closed

Type inference resulting in incorrect type for function result #14798

magnushiie opened this issue Mar 22, 2017 · 5 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@magnushiie
Copy link
Contributor

TypeScript Version: 2.2.1

Code

function functionReturningVoid() {
}
function functionReturningNumber() {
  return 0;
}
function returnValueFrom<R, F extends (() => R)>(f: F) {
  return f();
}
const rVoid = returnValueFrom(functionReturningVoid); // Argument of type '() => void' is not assignable to parameter of type '() => {}'.
const rNumber: number = returnValueFrom(functionReturningNumber); // Type '{}' is not assignable to type 'number'.

Expected behavior:
Compiles successfully.

Actual behavior:
Compiles with the above errors noted in comments.

@magnushiie
Copy link
Contributor Author

Note: it's valid to assign the value of a void-returning function, e.g. the following compiles:

const v = functionReturningVoid();

@magnushiie magnushiie changed the title Type inference resulting in incorrect types for function arguments Type inference resulting in incorrect type for function result Mar 22, 2017
@magnushiie
Copy link
Contributor Author

This might be related to #5884 and #5254 (for which #5884 was closed in favor of) but as all the types are specified at each line, it seems it's not a problem that call site inference is not taking place, but rather that R is widened unneccesarily?

@akaRem
Copy link

akaRem commented Mar 22, 2017

@magnushiie
Have you tried this?
(I've checked this snippet on http://www.typescriptlang.org/play/)

function functionReturningVoid()  {
}
function functionReturningNumber() {
  return 0;
}
function returnValueFrom<R>(f: () => R): R {
  return f();
}
const rVoid = returnValueFrom(functionReturningVoid); // ok
const rNumber : number = returnValueFrom(functionReturningNumber); // ok

Or are you trying to illustrate some more generic problem?

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Mar 22, 2017
@magnushiie
Copy link
Contributor Author

Ah, yes, this solves it. I thought I had a more generic instance where this would not be a solution, but I can't find it anymore.

@RyanCavanaugh
Copy link
Member

Constraints don't produce an inference site, so there's nothing to infer R from.

@microsoft microsoft locked and limited conversation to collaborators Jun 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants