Skip to content

Type argument inference doesn't seem to work correctly with generic type aliases #5417

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
seanchas116 opened this issue Oct 27, 2015 · 5 comments
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@seanchas116
Copy link
Contributor

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)

type Maybe<T> = T | void;

function get<T>(x: Maybe<T>): T {
    return null; // just an example
}

let foo: 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'.

Example with generic interface (works)

interface Maybe<T> {}

function get<T>(x: Maybe<T>): T {
    return null;
}

let foo: Maybe<string>;
get(foo).toUpperCase();

Version

1.8.0-dev.20151027
@mhegazy
Copy link
Contributor

mhegazy commented Nov 13, 2015

The problem is not type alaises. the issue here is that foo is of type string|void. here is a simpler example:

function get<T>(x: T | void): T {
    return null; // just an example
}

let foo: string | void;
get(foo).toUpperCase(); // T is still string|void

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Nov 13, 2015
@seanchas116
Copy link
Contributor Author

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?

@seanchas116
Copy link
Contributor Author

Actually, I tried this example first to achieve simple non-nullability:

type Maybe<T> = T | void;

function exists<T>(x: Maybe<T>): x is T {
    return x != null;
}

let foo: Maybe<string>;
foo.toUpperCase(); // error
if (exists(foo)) {
  foo.toUpperCase(); // OK
}

@mhegazy
Copy link
Contributor

mhegazy commented Nov 13, 2015

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.

@ahejlsberg
Copy link
Member

Now fixed by #5738.

@mhegazy 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
@mhegazy mhegazy added this to the TypeScript 1.8 milestone Nov 24, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants