You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Aliasing complex type compositions to short names within generic declarations would help with readability and refactoring, while allowing them to depend on other Generics. For example a class declaration could define types to be used within its own block for reuse with a short name.
An existing complex generic class declaration from a project I'm working on like...
...could be extended to define named types Item and Key<Name> which otherwise re-appear in full in almost every method in its definition. Below you can see a draft declaration using imaginary syntax which re-uses the as and infer keywords for this purpose...
This would not fundamentally change the expressivity of the language, but help with e.g. changing how those named types are composed in a single place and making it easier to read code that depended on them.
🔍 Search Terms
List of keywords you searched for before creating this issue...
alias generic types
✅ Viability Checklist
[✅] 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, new syntax sugar for JS, etc.)
Allowing aliases to appear in generic definitions means complex types can be defined in one place, even if they are dependent on Generic bindings, rather than duplicated in every location they are used.
💻 Use Cases
Below is an example drawn from an interface defined against the zod library, a schema library imported to deliver both runtime and compile-time checks. The class therefore directly refers to the schema AND the types derived from the schema, and crucially there is a 'type lookup' ListDefLookup which uses a lookup of zod runtime objects as a workaround for higher-order-types being absent. No doubt similarly complex types have arisen in other use cases. The implementation works fine, but is super-hard to read.
It sometimes seems like Item extends z.infer<I> = z.infer<I> would be equivalent, but this introduces an additional 'loose' type that commonly creates compile-time errors, when the compiler correctly detects that some class might have narrowed the type by overriding the default set by = so the types are not reliably equal.
Ideally it would be possible to simplify the abstract class Vault below to alias the composition z.infer<I> as Item once, and then refer to it throughout.
Thanks, @MartinJohns agreed the intent is the same with the syntax proposal different. If a feature was landed to allow new named types within a generic context (i.e. that could compose new named types from other types exploiting that generic context) I would use it to solve these problems whatever the syntax.
Suggestion
Aliasing complex type compositions to short names within generic declarations would help with readability and refactoring, while allowing them to depend on other Generics. For example a class declaration could define types to be used within its own block for reuse with a short name.
An existing complex generic class declaration from a project I'm working on like...
...could be extended to define named types
Item
andKey<Name>
which otherwise re-appear in full in almost every method in its definition. Below you can see a draft declaration using imaginary syntax which re-uses theas
andinfer
keywords for this purpose...See the fuller example below.
This would not fundamentally change the expressivity of the language, but help with e.g. changing how those named types are composed in a single place and making it easier to read code that depended on them.
🔍 Search Terms
List of keywords you searched for before creating this issue...
✅ Viability Checklist
⭐ Suggestion
📃 Motivating Example
Allowing aliases to appear in generic definitions means complex types can be defined in one place, even if they are dependent on Generic bindings, rather than duplicated in every location they are used.
💻 Use Cases
Below is an example drawn from an interface defined against the zod library, a schema library imported to deliver both runtime and compile-time checks. The class therefore directly refers to the schema AND the types derived from the schema, and crucially there is a 'type lookup' ListDefLookup which uses a lookup of zod runtime objects as a workaround for higher-order-types being absent. No doubt similarly complex types have arisen in other use cases. The implementation works fine, but is super-hard to read.
It sometimes seems like
Item extends z.infer<I> = z.infer<I>
would be equivalent, but this introduces an additional 'loose' type that commonly creates compile-time errors, when the compiler correctly detects that some class might have narrowed the type by overriding the default set by=
so the types are not reliably equal.Ideally it would be possible to simplify the abstract class
Vault
below to alias the compositionz.infer<I> as Item
once, and then refer to it throughout.Class without aliasing
...and this terser form is drafted using the keyword
as
within the class's generic declaration....Class using (imaginary)
as
aliasing syntaxBelow are the types referenced by the example in case they are needed to make sense of it.
The text was updated successfully, but these errors were encountered: