Skip to content

Better support for type bounds in generics #13337

Closed
@jklmli

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

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