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
Hi, I found that the following example that uses type argument inference and generic type alias doesn't compile:
Example with generic type alias (does not work)
typeMaybe<T>=T|void;functionget<T>(x: Maybe<T>): T{returnnull;// just an example}letfoo: Maybe<string>;get(foo).toUpperCase();// I think T == string in this function call...
Error
test.ts(8,10): error TS2339: Property 'toUpperCase' does not exist on type 'string | void'.
Thanks! I now see the meaning of the error message.
But isn't this a inconsistent behavior between generic type aliases and generic interfaces/classes?
Actually, I tried this example first to achieve simple non-nullability:
typeMaybe<T>=T|void;functionexists<T>(x: Maybe<T>): x is T{returnx!=null;}letfoo: Maybe<string>;foo.toUpperCase();// errorif(exists(foo)){foo.toUpperCase();// OK}
clever :) a word of caution, trying to wrestle in non-nullable types in the system might be a tedious and brittle thing to do. For future references implementing non-nullable types is tracked by #185.
mhegazy
added
Bug
A bug in TypeScript
Fixed
A PR has been merged for this issue
and removed
Question
An issue which isn't directly actionable in code
labels
Nov 24, 2015
Hi, I found that the following example that uses type argument inference and generic type alias doesn't compile:
Example with generic type alias (does not work)
Error
Example with generic interface (works)
Version
The text was updated successfully, but these errors were encountered: