Skip to content

Better support for type bounds in generics #13337

Description

@jklmli

This is currently valid, legal TypeScript:

class Container<A> {
  constructor(private value: A) {}

  act<B extends A>(b: B) { /*...*/ }
}

But reversing the type bound in act doesn't work, i.e.

act<A extends B>(b: B) { /*...*/ }

is not valid.

Ideally, the class's generic parameter can be used in any position.


Alternatively, we could support lower type bounds through a new keyword, narrows:

act<B narrows A>(b: B) { /*...*/ }

(meaning, B is a supertype of A).

Another possible alternative syntax:

act<B where { A extends B }>(b: B) { /*...*/ }

Examples of implementations in other languages:
http://rustbyexample.com/generics/where.html
http://docs.scala-lang.org/tutorials/tour/lower-type-bounds.html

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions