Skip to content
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

Infer types from generic type parameters of base types #1396

Closed
jdaley opened this issue Dec 8, 2014 · 3 comments
Closed

Infer types from generic type parameters of base types #1396

jdaley opened this issue Dec 8, 2014 · 3 comments
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@jdaley
Copy link

jdaley commented Dec 8, 2014

TypeScript should be able to infer type information using the generic type parameters of an extended/implemented generic class or interface.

Example:

interface Apple { }
interface Banana { }
interface Orange { }

interface Tree<TFruit> { }

class AppleTree implements Tree<Apple> { }
class BananaTree implements Tree<Banana> { }
class OrangeTree implements Tree<Orange> { }

function pickFrom<TFruit>(tree: Tree<TFruit>): TFruit {
    return null;
}

var tree = new AppleTree();
var fruit = pickFrom(tree);

fruit is inferred to be type {} but it should be Apple.

Worth noting, if tree is declared to be Tree<Apple> instead than a type that extends Tree<Apple>, then fruit is correctly inferred to be Apple.

@DanielRosenwasser
Copy link
Member

I believe this is a consequence of us failing to find candidates for TFruit in the implementing classes.

Note that if you have a property in Tree that uses TFruit, this should work out. For example:

interface Tree<TFruit> { fruit: TFruit[] }

class AppleTree implements Tree<Apple> { fruit: Apple[] }
class BananaTree implements Tree<Banana> { fruit: Banana[] }
class OrangeTree implements Tree<Orange> { fruit: Orange[] }

It might be worth linking to #360 here.

@jdaley
Copy link
Author

jdaley commented Dec 8, 2014

@DanielRosenwasser Thanks for pointing that out. Not ideal, but I could add a dummy property as a workaround.

@danquirk
Copy link
Member

danquirk commented Dec 8, 2014

In general:

  • Do not define types with no members.
  • Do not define generic types that don't use the type parameter type.

Otherwise you will get results like this and all sorts of other undesirable behavior. The inference works correctly as long as there's actual data to make inferences from. You surely do not want a type hierarchy that works this way:

var a: Apple = "yay";
var tree: AppleTree = 1;

Your types need to actually do something or contain some data to be meaningful.

@danquirk danquirk closed this as completed Dec 8, 2014
@danquirk danquirk added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Dec 8, 2014
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

3 participants