Description
I'm keeping this relatively brief and loose for now as I haven't thought it through in excruciating detail yet, I may add detail later to flesh this out into more of a formal proposal.
There's a desire to have some form of type switching in the type system. I think #12424 is the main proposal that tackles this. This suggestion is yet another potential approach to that issue, as well as potentially adding some other little niceties to the type system.
If type declarations could be overloaded in much the same manner as functions, this would allow for type switching in a manner that's already familiar within the language. The rules would effectively be the same as that of functions.
- All overloads must be directly adjacent so that they have order
- The first matching signature is used to resolve the type
- Overload signatures may have different lengths
- If no signatures match, it's an error just as it is currently
type Foo<T extends number> = {aNumber: number};
type Foo<T extends boolean> = {aBoolean: boolean};
type Foo<T extends any> = {aDefault: T};
type FooNumber = Foo<number>; // = {aNumber: number};
type FooString = Foo<string>; // = {aDefault: string};
This idea was spawned in #17325, credit to @SimonMeskens for wanting to discuss it properly and pushing me to post it.