TypeScript Version: 3.8.2
Search Terms: map, over, utility, type, result, keys, extract
Code
The following helper function is functioning beautifully for me:
export type ComputeUnionValue<V extends AnyCodec[]> = {
[i in Extract<keyof V, number>]: GetValue<V[i]>;
}[Extract<keyof V, number>];
I am attempting to simplify this helper, which incorporates Extract<keyof V, number>
in two instances.
export type ComputeUnionValue<V extends AnyCodec[], E = Extract<keyof V, number>> = {
[i in E]: GetValue<V[i]>;
}[E];
The use of E
in [i in E]
results in an error:
Type 'E' is not assignable to type 'string | number | symbol'.
Type 'E' is not assignable to type 'symbol'.ts(2322)
Furthermore, the usage of V[i]
leads to errors:
Type 'V[i]' does not meet the constraint 'AnyCodec'.
Type 'V[E]' is not suitable for type 'Codec<VType, unknown>'.
Type 'AnyCodec[][E]' is not compatible with type 'Codec<VType, unknown>'.ts(2344)
and
Type 'i' cannot be used as an index for type 'V'
I opted for using E =
to include that new type within the scope without it being an optional argument. However, treating it as optional seems to impact the "guarantees", causing the compatibility issues mentioned above. Is there a way to define the E
type––specific to this utility function––in a manner that eliminates any possibility of type incompatibility?