You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Generic type defaulting to {} instead of any #7505
Expected behavior:
The type of test is any so there is no compiler error.
Actual behavior:
I get the following error message
myFile.ts(9,18): error TS2339: Property 'a' does not exist on type '{}'.
Use Case
I run into this issue whenever I'm working on an Angular project and using the $http service. The type definition for $http.get() is as follows
myFunc (and $http.get, it looks like) doesn't have any arguments to infer T from. So type inference will end up assigning the type {} if it's not specified. This behaviour is by design.
I don't think there's any alternative to specifying the type you're getting from $http.get. TypeScript doesn't infer types from later usage of test, for example.
When you don't specify a type everywhere else in TypeScript the type defaults to any so this behavior seems counter intuitive to me. But yeah I can just specify the type to workaround it.
Rob (@robianmcd) I think it's because "T" inside of a function acts like {} -- you can't see any properties on it, for example. So if you return T, and the compiler never inferred any actual type for it, it sort of makes sense that it comes out as {}.
Also, unlike the other cases where there is no type specified, there is a type variable here -- T -- which the compiler infers a type for. {} indicates failure rather than any. any would be easier to use, though.
Activity
sandersn commentedon Mar 14, 2016
myFunc(and$http.get, it looks like) doesn't have any arguments to inferTfrom. So type inference will end up assigning the type{}if it's not specified. This behaviour is by design.I don't think there's any alternative to specifying the type you're getting from
$http.get. TypeScript doesn't infer types from later usage oftest, for example.robianmcd commentedon Mar 14, 2016
When you don't specify a type everywhere else in TypeScript the type defaults to
anyso this behavior seems counter intuitive to me. But yeah I can just specify the type to workaround it.mhegazy commentedon Mar 14, 2016
#6354 would be a better solution here.
mhegazy commentedon Mar 14, 2016
also, you can achieve the same result today by defining an overload.
sandersn commentedon Mar 14, 2016
Rob (@robianmcd) I think it's because "T" inside of a function acts like
{}-- you can't see any properties on it, for example. So if you return T, and the compiler never inferred any actual type for it, it sort of makes sense that it comes out as{}.Also, unlike the other cases where there is no type specified, there is a type variable here --
T-- which the compiler infers a type for.{}indicates failure rather thanany.anywould be easier to use, though.robianmcd commentedon Mar 14, 2016
Mohamed Hegazy (@mhegazy) yeah the issue you linked to would be a good solution. Also I didn't think of overloading the function like that. thanks