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

New keyword: alias, a local type definition replacer #40780

Closed
5 tasks done
SrBrahma opened this issue Sep 25, 2020 · 2 comments
Closed
5 tasks done

New keyword: alias, a local type definition replacer #40780

SrBrahma opened this issue Sep 25, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@SrBrahma
Copy link

SrBrahma commented Sep 25, 2020

Search Terms

alias, type alias

Suggestion

When writing a complex type, often the code becomes a "type spaghetti", being hard and time consuming to identify what all those AAA<BBBBB[] extends ? BB: >... and it is even worse when we have to use a same lengthy type that is used in more than once place inside a generic type.

My suggestion is to create a new Typescript keyword, the alias or something like that, that would work like a "local const" for types or a simple type replacer. Like C macros for types.

Use Cases

Would reduce code size and the time to understand the code, and improve code readability and comprehension.

It is possible to create auxiliary types, but being a type necessarily generic, we still have to pass the generic types for every time we call them.

Examples

type MyType<ChildrenOrType extends ... , Key extends ... , Extra extends ... > = ...<...< {
  a: AnotherType<ChildrenOrType, Key, Extra>;
  b: (data: AnotherType<ChildrenOrType, Key, Extra>): string;
  c: (data: number) => AnotherType<ChildrenOrType, Key, Extra>
}>...>...

Even reducing the generic types to the famous T,Q,U,P etc, which reduces the code length but also makes it more hard to understand what each type is for, still isn't good to use <T, Q, P, X> for each time you want to mention the generic types.

type MyType<CT extends ... , K extends ... , E extends ... > = ...<...< {
  a: AnotherType<CT, K, E>;
  b: (data: AnotherType<CT, K, E>): string;
  c: (data: number) => AnotherType<CT, K, E>;
}>...>...

Here is my suggestion:

type MyType<ChildrenOrType extends ... , Key extends ... , Extra extends ... > = ...<...< {
  alias AliasAnotherType = AnotherType<ChildrenOrType, Key, Extra>
  a: AliasAnotherType;
  b: (data: AliasAnotherType): string;
  c: (data: number) => AliasAnotherType
}>...>...

Or using letters for types

type MyType<CT extends ... , K extends ... , E extends ... > = ...<...< {
  alias AliasAnotherType = AnotherType<CT, K, E>
  a: AliasAnotherType;
  b: (data: AliasAnotherType): string;
  c: (data: number) => AliasAnotherType
}>...>...

It would also fit well in recursive types.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Maybe it could accept generics too.

A thing to think about this is: what should be the scope of those alias? Should they work/apply anywhere inside the type definition? Local inside (), {}, <>?

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 28, 2020
@RyanCavanaugh
Copy link
Member

See #30979. Thanks!

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants