Allow mapped type to declare functions
See original GitHub issueSuggestion
đ Search Terms
- mapped types
- instance member function
- instance member property
- instance method
- subclass constructor
- overriding
â Viability Checklist
My suggestion meets these guidelines:
- 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
đ Motivating Example
In our universe, there are two kinds of animals, dogs and cats.
type Animal = 'dog' | 'cat';
Some people can handle dogs. Some people can handle cats. Some people can even handle both. But if you canât handle dogs or cats, youâre just not a handler. We can express this generically in TypeScript.
type Handler<A extends Animal> = {[animal in A as `handle${Capitalize<animal>}`]: (animal: animal) => void};
Letâs say you have a dog handler that doesnât do anything interesting.
class BoringDogHandler implements Handler<'dog'> {
handleDog(dog : `dog`) {}
}
You could also have someone who can handle both and keeps a record of how many animals handled.
class CountingUniversalAnimalHandler implements Handler<Animal> {
count = 0;
handleDog() {this.count++;}
handleCat() {this.count++;}
}
Some dog handlers are well known for broadcasting to the world whenever they are handling a dog. Since this is such a common thing, weâd like to create a mixin for it.
function NoisyDogHandler<H extends {new (...args: any[]): Handler<'dog'>}>(base: H) {
return class extends base {
handleDog(dog : `dog`) {
console.log("Handling dog");
return super.handleDog(dog);
}
};
}
Looks innocuous enough. But this doesnât work! Typescript informs us that
Class
Handler<"dog">
defines instance member propertyhandleDog
, but extended class(Anonymous class)
defines it as instance member function. (2425)
If we were allowed to declare that the handle${Capitalize<A>}
members were not mere properties, but actually methods, TypeScript would not fear letting us override them. The natural syntax would look like this, which is currently not allowed:
type Handler<A extends Animal> = {[animal in A as `handle${Capitalize<animal>}`](animal: animal): void};
A subtle distinction for a subtle distinction.
đť Use Cases
What do you want to use this for?
Mix-ins are powerful, and when they work theyâre fantastic, but theyâre currently somewhat brittle to work with.
This is one of the pieces of the puzzle needed for classes extending constructors of mapped types to override methods (#27689). The other piece needed is to be able to keep the member function status of methods obtained from type indexing (#38496, c.f. #35416, and #46802).
What shortcomings exist with current approaches?
For small enough use cases, you can do the mapping âby handâ:
type Animal = 'dog' | 'cat';
type DogHandler = {handleDog(dog: 'dog'): void};
type CatHandler = {handleCat(cat: 'cat'): void};
type BothHandler = DogHandler & CatHandler;
type Handler = DogHandler | CatHandler;
type HandlerFor<A extends Animal> = ('dog' extends A ? DogHandler : {}) & ('cat' extends A ? CatHandler : {})
Obviously this doesnât scale well if you create multiple methods with Animal
instead of just one, or Animal
has many more cases. Less obviously, there isnât a really satisfactory way to implement a generic method that takes an animal and handler, calls the right handle method, ensures at compile time that the handler can handle the animal, and doesnât involve casting. See this playground for several attempts.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:9
- Comments:6 (1 by maintainers)
Based on the use case Iâm inclined to just discard the
... defines instance member property ..., but extended class ... defines it as instance member function.
error when the base class property is created through a mapped type. Thoughts?Oh, but if you do discard the error message, make sure it discards it for subclasses as well.