Skip to content

Question: Why can't type parameters be used inside type parameters? #6224

Description

@ryasmi

Hey all, hoping to be educated by someone. I think I need to reuse type parameters inside type parameters (see example code below, specifically the reuse of Y), but this is firstly not allowed by TypeScript (and perhaps other languages too), and secondly it seems "odd". So I'm wondering, why can't type parameters be used inside type parameters? Is there a solution to this I'm missing?

class A {
  // ...
}

class B<Z extends A> {
  public a: Z;
  // ...
}

class C<Y extends A, X extends B<Y>> {
  public a: Y;
  public b: X;
  // ...
}

Activity

  1. ryasmi commented on Dec 23, 2015

    @ryasmi
    Author

    Thanks for your response Kagami Sascha Rosylight (@saschanaz). That's the solution I'm using to overcome this at the moment, I'm just wondering if there is perhaps a better one, since to extend that class using a subclass of B I must override the parameter (see class E below).

    class D<Z> extends B<Z> {
      // ...
    }
    
    class E<Y> extends C<Y> {
      public b: D<Y>;
      // ...
    }
  2. ryasmi commented on Dec 23, 2015

    @ryasmi
    Author

    Has Kagami Sascha Rosylight (@saschanaz) deleted their comment?

    I think that is to prevent circular reference. Will this solve your problem?

    class C<Y extends A> {
      public a: Y;
      public b: B<Y>
    }
  3. saschanaz commented on Dec 23, 2015

    @saschanaz
    Contributor

    @ryansmith94 Ah, I thought my answer was not 100% right so I deleted it, sorry.

  4. ryasmi commented on Dec 23, 2015

    @ryasmi
    Author

    Ah no worries, I wouldn't worry about posting something you're unsure about as long as you say that you're not sure 😄

  5. DanielRosenwasser commented on Dec 23, 2015

    @DanielRosenwasser
    Member

    Try installing the nightly of TypeScript (npm install -g typescript@next) where we should now support type parameters referencing other type parameters (see #5949 and https://github.com/Microsoft/TypeScript/wiki/Roadmap#18).

  6. ryasmi commented on Dec 23, 2015

    @ryasmi
    Author
  7. locked and limited conversation to collaborators on Jun 19, 2018
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

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions