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

Return type inference of overloaded function when argument is known #27027

Closed
KSXGitHub opened this issue Sep 11, 2018 · 3 comments
Closed

Return type inference of overloaded function when argument is known #27027

KSXGitHub opened this issue Sep 11, 2018 · 3 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@KSXGitHub
Copy link
Contributor

KSXGitHub commented Sep 11, 2018

TypeScript Version: 3.0.3

Search Terms:

Code

declare function fn(x: 'a'): 0
declare function fn(x: 'b'): 1
declare function fn(x: 'c'): 2
declare function fn(x: 'D'): 3

// expected: type One = 1
// received: type One = never
type One = (typeof fn) extends (x: 'b') => infer Return ? Return : never
const one: One = 1

Expected behavior:

One should be 1

Actual behavior:

One is never

Playground Link: link

Related Issues: #26591

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Sep 11, 2018
@RyanCavanaugh
Copy link
Member

Overloads aren't resolved during inference (only the last is examined), nor are they expanded out combinatorially. In general for any given sequence of specific overloads like this there "should" be a final signature like (x: 'a' | 'b' | 'c' | 'D'): 0 | 1 | 2 | 3, which will be the one used during inference. This won't give you the most specific type, but would prevent hitting never here

@ghost
Copy link

ghost commented Oct 10, 2018

Example without conditional types:

function call<T>(f: () => T): T {
	return f();
}

declare function f(): string;
declare function f(x: string): void;
const x = call(f); // Want string, got void

@huan
Copy link

huan commented Nov 7, 2021

The "Example without conditional types" posted by @andy-ms is still true under TypeScript v4.4.4

I believe this is definitely typing system bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants