microsoft / microsoft/TypeScript
Support shorthands when specializing generic functions at the type level
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Suggestion
🔍 Search Terms
- shorthands
- generic
- specialization
✅ 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.)
- This feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
Basically support for syntax like this:
const someObject = {
foo<number>,
bar<string>
};
One can already specialize a generics cheaply like this:
const specializedFoo = foo<number>;
But this kind of specialization doesn't work well with shorthand syntax, so rather than writing what I suggested one has to write the more verbose:
const someObject = {
foo: foo<string>,
bar: bar<number>
};
Basically imo this convenience feature for specializing a generic should compose with other existing convenience syntax.
📃 Motivating Example
I have a little parser generator, problem is for many grammars the generic <T> type that most exported composable functions accept is inferred in incompatible ways between usages of those composable functions throughout a grammar, so having a way to return specialized functions, where the value of the type has been already been provided consistently for all those functions, would simplify some scenarios a lot.
But I can't write something like this:
const grammar = <T, U> ( fn: ({ match: match<T>, and: and<T>, ... }) => U ): U => {
return fn ({
match<T>,
and<T>,
...
});
};
I have to write this:
const grammar = <T, U> ( fn: ({ match: match<T>, and: and<T>, ... }) => U ): U => {
return fn ({
match: match<T>,
and: and<T>,
...
});
};
Arguably this kind of syntax should be supported for types also, so that I could even just write this, ideally:
const grammar = <T, U> ( fn: ({ match<T>, and<T>, ... }) => U ): U => {
return fn ({
match<T>,
and<T>,
...
});
};
💻 Use Cases
Simplifying the code in the example above.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No TypeScript source file, test, or entry point is named; begin by reviewing the proposed shorthand examples and the linked grammex use case. Done would require agreeing on the syntax and semantics, implementing support for specialized generic functions in object and type shorthands, and adding coverage for the examples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100