Skip to content

Type inference incorrect for "union of functions" types #27690

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

Closed
FremyCompany opened this issue Oct 10, 2018 · 1 comment
Closed

Type inference incorrect for "union of functions" types #27690

FremyCompany opened this issue Oct 10, 2018 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@FremyCompany
Copy link

FremyCompany commented Oct 10, 2018

TypeScript Version: 3.1.2
Search Terms: function inference

Code

interface Prop<T> {
	(): T
	(v:T): void
}
function cachedCast<I,O>(
    input$: ()=>I, 
    convertInput: (i:I)=>O
): ()=>O { /*…*/return null as any/*…*/ }

let title$ : Prop<string>
let uppercase_title$ = cachedCast(title$, title=>title.toUpperCase()); // Error: `void.toUpperCase` is undefined

Expected behavior:

The title variable should be of type string

Actual behavior:

The title variable ends up of type void.

Playground Link: https://www.typescriptlang.org/play/index.html#src=interface%20Prop%3CT%3E%20%7B%0D%0A%09()%3A%20T%0D%0A%09(v%3AT)%3A%20void%0D%0A%7D%0D%0Afunction%20cachedCast%3CI%2CO%3E(%0D%0A%20%20%20%20input%24%3A%20()%3D%3EI%2C%20%0D%0A%20%20%20%20convertInput%3A%20(i%3AI)%3D%3EO%0D%0A)%3A%20()%3D%3EO%20%7B%20%2F*%E2%80%A6*%2Freturn%20null%20as%20any%2F*%E2%80%A6*%2F%20%7D%0D%0A%0D%0Alet%20title%24%20%3A%20Prop%3Cstring%3E%0D%0Alet%20uppercase_title%24%20%3D%20cachedCast(title%24%2C%20title%3D%3Etitle.toUpperCase())%3B%20%2F%2F%20Error%3A%20%60void.toUpperCase%60%20is%20undefined

Related Issues: I think this might be the same problem as issue #27027 but it is different in the sense that here I define an interface, not a function overload (though I guess it might be the same thing under the hood?)

I have the impression that this used to work at some point, but I might be wrong. Or maybe it used to be inferred as any and I never noticed?

@ghost
Copy link

ghost commented Oct 10, 2018

Yeah, duplicate of #27027

@ghost ghost added the Duplicate An existing issue was already created label Oct 10, 2018
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

2 participants