Closed
Description
Search Terms
- implicit bracket
- mapped types generic
- nested generic
- syntax brackets
- syntax omit
- syntax parentheses
Suggestion
Support a simplified syntax for generic types that have only 1 type parameter. Specifically, Partial<Foo>
could also be written as Partial Foo
.
Use Cases
- This feature would allow people to mapped types that are easier to read. As it would be an opt-in syntax (you can still write generic types with
<>
) there should be no breaking change in code or workflows.- Complex/highly-nested types would have the most to gain from this, but it could be done on any generic type, so long as it has only 1 type parameter
- In certain cases, this feature could also make for more readable error messages involving generic types.
- This is partially motivated by Scala's support for omitting parentheses in methods with only 1 argument:
object.method(param) // same as object method param
- While I would welcome such a feature for runtime values in TypeScript, here I am only concerned with the equivalent for generic types, the altering of which has no effect on the output JS.
- There is no drawback to using
<>
in general, but for sufficiently nested types it can severely affect readability.
Examples
Simple:
With brackets | Without |
---|---|
Partial<Foo> |
Partial Foo |
NonNullable<string> |
NonNullable string |
Set<number> |
Set number |
Complex:
With brackets | Without |
---|---|
Array<Promise<{}>> |
Array Promise {} |
NonNullable<Partial<Config>[T]> (from #31675) |
NonNullable Partial<Config>[T] orNonNullable (Partial Config)[T] |
Promise<NonNullable<Partial<Record<string, any>>>>> |
Promise NonNullable Partial Record<string, any> |
The following would be disallowed, namely because they involve types with 2 or more generic parameters:
Record number any
Map string boolean
Pick Foo 'bar'
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript codeThis wouldn't change the runtime behavior of existing JavaScript codeThis could be implemented without emitting different JS based on the types of the expressionsThis 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.
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
dragomirtitian commentedon Oct 13, 2019
I really don't find
Promise NonNullable Partial Record<string, any>
more readable than the current syntax.Also adding two syntaxes for the same thing doesn't seem like a good idea. There is not savings in terms of typing, it goes against this is done in other languages that use
<>
for generics (C#, Java, C++, don't have this space syntax), and the readability claim seems highly debatable to me.(Just my 2 cents on the matter, happy to be wrong, maybe others will find this new syntax useful)
Jessidhia commentedon Oct 15, 2019
I find that this would be more confusing specifically because of the different meaning space-separated identifiers have in other languages, like Scala and Haskell.
RyanCavanaugh commentedon Oct 15, 2019
Aesthetic preferences aside, doing this would severely impede our ability to add unary prefix type operators (e.g.
readonly
) in the future. There'd need to be some better upside for the loss of that flexibility.ShanonJackson commentedon Oct 16, 2019
I also find this less readable, i feel like where possible we should standardize a uniform way and not allow (when possible) alternative ways of doing the same thing. I feel like it leads to increased cognitive overhead in reading code, and adds a steeper learning curve to the language.