microsoft / microsoft/TypeScript
Feature: Composing spread operator on Union types and static_key function to enable generic capability
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Related issues: #13542 #15759 (slightly different take and example on closed issues)
I originally posted on issue #15759 but decided that they were different enough to be looked at separately and that issue doesn't address a generic capability. Look back at https://github.com/Microsoft/TypeScript/issues/15759#issuecomment-314157616 for clunky workaround examples.
Proposal
- Add spread operator
...to string literal union types acting as an string array or generator. TypeTapplying spread operator will result in typeT[]. For example:
type Group = 'red' | 'blue' | 'green';
const groups = ...Group; // groups has type of Group[] with values ['red', 'green', 'blue']
- Add static function
static_keyorkeyofthat transforms a static interface to a string literal union of all combined types. For example:
interface Groups<T, U, V> {
red: T;
green: U;
blue: V;
}
type groups = static_key(Groups); // 'red' | 'blue' | 'green'
- Composing both can result in powerful generic operations. I feel that this could bring a unique capability that string enums could not and may have been overlooked. It would be very powerful for a
Recordtype or interface where all fields are the same type. Here is an example:
interface GroupDescription {
name: string;
description: string;
}
type Group = 'red' | 'blue' | 'green';
type Groups = { [K in Group]: GroupDescription };
function initGroups(groups: Groups) {
for(const group of ...static_key(Groups)) { // group would be type Group or 'red' | 'blue' | 'green'
processGroup(name); //Generically Process property
}
}
function processGroup(group: Group) { }
I noticed I don't need ...static_key but instead directly use the spread operator on the union type ...Group. The point is that they compose well together and they add generic capability.
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
The issue names no files, tests, or entry points. Start by reading the proposal and related issues #13542 and #15759, including the linked workaround comment, then establish an agreed design for union spreading and static_key. Done requires a maintainer-approved scope and implementation criteria.
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
- Needs clarification
- Newbie friendliness
- 25/100