-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Description
TypeScript Version: 3.0.1
Search Terms: return type inference
Code
My specific instance of this problem can be found here
Scroll to the very bottom to see a comment about it.
I couldn't figure out a minimal example but it looks like,
const result1 = foo(/*arg*/); //Inferred type is correct
const result2 = foo(result1); //Inferred type is wrong; shows `any`However, if I explicitly declare the type of result1, it works,
const result1 : number = foo(/*arg*/); //Explicitly declared
const result2 = foo(result1); //Inferred type is correctOr, if I do this very weird thing,
//Both foo1<>() and foo2<>() are the same, I just copy pasted foo<>() and renamed
declare function foo1</*Complicated Type*/> (/*Params*/) : /*Complicated Type*/;
declare function foo2</*Complicated Type*/> (/*Params*/) : /*Complicated Type*/;
const result1 = foo1(/*arg*/); //Inferred type is correct
const result2 = foo2(result1); //Inferred type is correctTrying a type alias fails,
declare function foo</*Complicated Type*/> (/*Params*/) : /*Complicated Type*/;
type FooDelegate = </*Complicated Type*/>(/*Params*/) => /*Complicated Type*/;
const foo1 : FooDelegate = foo;
const foo2 : FooDelegate = foo;
const result1 = foo1(/*arg*/); //Inferred type is correct
const result2 = foo2(result1); //Inferred type is wrong; shows anyBut this also works,
declare function foo</*Complicated Type*/> (/*Params*/) : /*Complicated Type*/;
const foo1 : </*Complicated Type*/>(/*Params*/) => /*Complicated Type*/ = foo;
const foo2 : </*Complicated Type*/>(/*Params*/) => /*Complicated Type*/ = foo;
const result1 = foo1(/*arg*/); //Inferred type is correct
const result2 = foo2(result1); //Inferred type is correctIt seems like if I rely on return type inference, I have to copy-paste the function type, and cannot re-use a previous declaration.
So, if I wanted to nest the results 4 times, I'd need foo1, foo2, foo3, foo4
Right now, I just either declare the return-type explicitly, every time the function is used, or I don't use the function at all.
Expected behavior:
Inferred type to not have any
Actual behavior:
Inferred type contains any