question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Permit type alias declarations inside a class

See original GitHub issue

I normally want my class declaration to be the first thing in my file:

module foo {

  /** My foo class */
  export class MyFoo {

  }
}

So when I want to declare an alias, I don’t like the fact that my class has been pushed down further:

module foo {

 type foobar = foo.bar.baz.FooBar;

  /** My foo class */
  export class MyFoo {

  }
}

I suggest the following be permitted:

module foo {

  /** My foo class */
  export class MyFoo {
    type foobar = foo.bar.baz.FooBar;
  }
}

Since type foobar is just a compile-time construct, and if people want to write it that way, then they should be permitted to do so. Note that the type foobar is private to the class. It should not be permissible to export foobar.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:148
  • Comments:75 (17 by maintainers)

github_iconTop GitHub Comments

319reactions
malibuzioscommented, Mar 20, 2016

I’m running into this pattern:

I have a generic class and would like to create a type alias that references a specialized generic type expression that is only valid for that particular class scope, for example instead of:

class GenericClass<T> {
  func1(arr: Array<{ val: T }>):  Array<{ val: T }> {
    ..
  }

  func2(arr: Array<{ val: T }>):  Array<{ val: T }> {
    ..
  }
...
}

It would be great if I could alias that complex type expression (Array<{ val: T }>) to a locally scoped type. e.g.

class GenericClass<T> {
  type SpecializedArray = Array<{ val: T }>;

  func1(arr: SpecializedArray): SpecializedArray {
    ..  
  }

  func2(arr: SpecializedArray): SpecializedArray {
    ...
  }
}

I’m not exactly sure how to effectively work around this. Both the solutions provided @RyanCavanaugh and the original one mentioned by @NoelAbrahams would still a require to parameterize the type alias. E.g:

type SpecializedArray<T> = Array<{ val: T }>;

But that’s not really what I’m looking for… The whole idea was to make it simpler and more readable… (also, if part of the workaround meant I had to use some strange merging between a class and a namespace I would rather just have nothing at all and write all the types verbosely).

76reactions
sztomicommented, Mar 21, 2018

Here’s an example where this feature would be very useful.

export class State<Constant, Trigger> {
  private _transitions: Map<Trigger,
                            Transition<State<Constant, Trigger>, Trigger>>;

  constructor(public value: Constant) {
    this._transitions = new Map<Trigger,
                                Transition<State<Constant, Trigger>, Trigger>>();
  }

could become

export class State<Constant, Trigger> {
  type TransitionT = Transition<State<Constant, Trigger>, Trigger>;
  type MapT = Map<Trigger, TransitionT>;

  private _transitions: MapT;

  constructor(public value: Constant) {
    this._transitions = new MapT();
  }

I’m sorry to see this proposal was declined because a good example was not provided on time. I hope this will be reconsidered.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Type Aliases vs Interfaces in TypeScript - DEV Community ‍ ‍
Type aliases work in a similar manner. Wherever you annotate a type by its alias, the alias will evaluate to the type(s) that...
Read more >
Is it possible to declare a nested type alias using a forward ...
No, you cannot put something inside of a class except by actually writing it inside the class definition. It's not an issue of...
Read more >
Aliases and typedefs (C++) - Microsoft Learn
You can use an alias declaration to declare a name to use as a synonym for a previously declared type. (This mechanism is...
Read more >
Type alias, alias template (since C++11) - cppreference.com
Like any template declaration, an alias template can only be declared at class scope or namespace scope. The type of a lambda expression ......
Read more >
PEP 613 – Explicit Type Aliases
Type aliases are valid within class scope, both implicitly ( x ) and explicitly ( y ). If the line should be interpreted...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found