Backbone Demo
const DirectedPropertySets = {
start: ["left", "top"] as const, // for DOMRectReadOnly
client: ["clientX", "clientY"] as const // for PointerEvent
};
type DirectedPropertySet<T> = {
[P in keyof typeof DirectedPropertySets]: typeof DirectedPropertySets[P][0] extends keyof T ? P : never;
}[keyof typeof DirectedPropertySets];
/**
* One of usage of this whole thing to show you why I'm making things like this
* Don't be bothered by this
*/
function getFromRect(rect: DOMRectReadOnly, key: DirectedPropertySet<DOMRectReadOnly>) {
const set = DirectedPropertySets[key];
/**
* const set: readonly ["left", "top"]
*/
return rect[set[0]]; // fine
}
/**
* This is the issue that produces an error
*/
function getFromEvent<T extends Event>(event: T, key: DirectedPropertySet<T>) {
const set = DirectedPropertySets[key];
/**
* const set: {
* start: readonly ["left", "top"];
* client: readonly ["clientX", "clientY"];
* }[DirectedPropertySet<T>]
*/
return event[set[0]]; // error
}
/**
* The previous one that has a change not to produce an error
*/
function getFromEvent<T extends Event>(event: T, key: DirectedPropertySet<Event>) {
const set = DirectedPropertySets[key];
/**
* const set: never
* it's okay to have type of never as I intended it
*/
return event[set[0]]; // fine
}
From how the type of local variable set
changes depending on the type of the parameter key
, I think that DirectedPropertySet<T>
in key: DirectedPropertySet<T>
just refer to the definition of the type DirectedPropertySet<T>
rather than actually applies the generic type T extends Event
into DirectedPropertySet<T>
.
What am I mistaking about Typescript's generic type?
From @jcalz's request, I'll demonstrate my actual usage of it as simply as possible. You'll see that actually there is another parameter in getFromEvent
.
// "pointermove" event listener
function onPointerMove(ev: PointerEvent) {
/**
* Only "client" is visible,
* which means DirectedPropertySet<T> of getFromEvent is resolved,
* but then why the DirectedPropertySets[key][axis]
* can't index "event" parameter of getFromEvent?
*/
const ... = getFromEvent(ev, "client", 0);
...
}
// parameter "axis" is added to index DirectedPropertySets[key]
function getFromEvent<T extends Event>(event: T, key: DirectedPropertySet<T>, axis: number) {
const property = DirectedPropertySets[key][axis];
// return event[property]; // produces an error
return event[property as Extract<typeof property, keyof T>];
}
Typescript's lead developer answered about this issue.
(Note that I gave him a different example, so type names and type declarations are a bit different.)
There's no way for TS to realize that the only kinds of things that can come out of Intersecting are keyof Ts
I understood this as TS cannot resolve some complicated parameter types.
DirectedPropertySet<T>
type resolves to either"client"
or"client" | "marginEnd"
. It will ONLY include"marginEnd"
as a possibility if the event typeT
has bothmarginRight
andmarginBottom
. We get an error when accessing a property that could bemarginRight
on an eventT
because not allT
s will have this property, but the propertyk[0]
can only ever bemarginRight
in cases where it DOES exist onT
.getFromEvent()
, though. Can you include some simple calls so I can see what you're trying to support? Clearly you care about the correlation between the first and second arguments, but I would like to see use cases to know if I can change the signatures to provide a better experience.