Repository navigation
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
Activity
RyanCavanaugh commentedon Aug 8, 2018
Can you boil this down to a standalone example? Just trimming down things from the linked file until there's nothing that doesn't need to be there should be enough. Thanks!
AnyhowStep commentedon Aug 19, 2018
It took me forever, but I finally found a minimal example,
TS Playground link
I have all the checkboxes ticked in the
Optionstab;noImplicitAny, strictNullChecks, strictFunctionTypes, strictPropertyInitialization, noImplicitThis, noImplicitReturnsThis behaviour is breaking my data validation library that has to validate the data type and value of nested objects in a multitude of ways. Right now, run-time validation works correctly but compile-time checks break because it infers
anyAnyhowStep commentedon Aug 19, 2018
I just played with it a bit more.
TS Playground link
So, it looks like the tooltip that gives the type inference is short-circuiting or something and just giving
any. But the actual type checking still goes all the way.So, it seems like it's somewhat related to #26238
In TS 3.0.1 with VS code (and on the Playground editor), all of a sudden, the tooltip that's supposed to give the inferred type is truncating text, and it looks like it also gives
anywith nested calls to the same function.Is there any way I can get it to just display the full type truthfully, without it truncating, or giving up and displaying
any? It's kind of a productivity killer when I have to second-guess the tools. They're telling me it's one thing but I check and it's another.I remember, in TS 2.9, there wasn't truncation on the tooltip displaying inferred types.
RyanCavanaugh commentedon Aug 20, 2018
Fix bugs in the compiler 😉
RyanCavanaugh commentedon Aug 20, 2018
Wesley Wigham (@weswigham) type display issue
AnyhowStep commentedon Aug 20, 2018
I've tried to look at the source code a few times, cursory glances only, and it just goes over my head. Hopefully I can figure this all out at some point.