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

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

Closed
ryasmi opened this issue Dec 23, 2015 · 6 comments
Closed

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

ryasmi opened this issue Dec 23, 2015 · 6 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@ryasmi
Copy link

ryasmi commented Dec 23, 2015

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;
  // ...
}
@ryasmi
Copy link
Author

ryasmi commented Dec 23, 2015

Thanks for your response @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>;
  // ...
}

@ryasmi
Copy link
Author

ryasmi commented Dec 23, 2015

Has @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>
}

@saschanaz
Copy link
Contributor

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

@ryasmi
Copy link
Author

ryasmi commented Dec 23, 2015

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

@DanielRosenwasser
Copy link
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).

@DanielRosenwasser DanielRosenwasser added the Question An issue which isn't directly actionable in code label Dec 23, 2015
@ryasmi
Copy link
Author

ryasmi commented Dec 23, 2015

Awesome! Thanks @DanielRosenwasser 👍

@ryasmi ryasmi closed this as completed Dec 23, 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
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants