-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for generic types on a index accessor #58258
Comments
This seems like just an exact duplicate of #17867. You can already write type SuperRecord<Key extends keyof any, Value, Overlay = {}> = {
[E in Key]: E extends keyof Overlay ? Overlay[E]: Value;
}; to get the generic behavior on an index signature's output type, nothing new is needed. |
@RyanCavanaugh Logically the method you suggested should behave exactly the same as the one I mentioned above. However when I tested with the code below, only the key and value part works correctly, the overlay does not. Invoking a function on the overlay produces an error message:
The code I used: type SuperRecord<Key extends keyof any, Value, Overlay = {}> = {
[E in Key]: E extends keyof Overlay ? Overlay[E]: Value;
};
interface K extends SuperRecord<string, string, { func1(): void; }> {
}
let k: K = ...
k["str"] = "val"; // works fine
k.func1(); // error
k.toString(); // works fine |
Right, you'd still need rest signatures. But type O = { func1(): void; };
interface K extends SuperRecord<keyof O, string, O> {
// TODO: Be able to uncomment.
// [...rest: string]: string
} |
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
index accessor
generic index accessor
β Viability Checklist
β Suggestion
Allow type parameters to be used for an index accessor. This allows users to declare additional supporting methods within an interface that has a string index accessor with a value set to a non
any
orFunction
type, by using an overlay interface.Example:
π Motivating Example
This fixes #58256 and #17867, without using an intersection type. This would allow the type to be declared as an
interface
, which means it can be implemented by aclass
or extended by anotherinterface
without any errors caused by conflicting properties.π» Use Cases
What do you want to use this for?
Various cases where an interface requires both an index accessor and other properties that could cause conflict in the type.
Furthermore some types in
lib.dom.d.ts
can be improved, for example astring
valued index accessor can be used for theStorage
interface without affecting other supporting functions.What shortcomings exist with current approaches?
It might slow down the type checking process.
What workarounds are you using in the meantime?
An intersection type like this:
which cannot be extended by an interface, or implemented by a class, due to property type conflicts.
The text was updated successfully, but these errors were encountered: