Closed as not planned
Description
π Search Terms
mapped tuple, mapped types
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about mapped types
β― Playground Link
π» Code
declare class Decoder<T> {
decode(arrayBuffer: ArrayBuffer): T;
}
type ValueTypeOf<T extends Decoder<any>> = T extends Decoder<infer R> ? R : never;
type StructDescriptor = ReadonlyArray<readonly [key: string, type: Decoder<any>]>;
type StructTypeFor<Descriptor extends StructDescriptor> = {
[K in (keyof Descriptor) & number as Descriptor[K][0]]: ValueTypeOf<Descriptor[K][1]>;
};
class StructDecoder<const Descriptor extends StructDescriptor> extends Decoder<StructTypeFor<Descriptor>> {
constructor(descriptor: Descriptor) {
super();
}
}
declare const i32Decoder: Decoder<number>;
declare const i64Decoder: Decoder<bigint>;
const structDecoder = new StructDecoder([
["a", i32Decoder],
["b", i64Decoder],
]);
const struct = structDecoder.decode(new ArrayBuffer(100));
// I expected this would work, but it does not
const v: number = struct.a;
π Actual behavior
The type of struct.a
has type number | bigint
, however it should only have type number
.
π Expected behavior
struct.a
should have type number
(and similarly struct.b
should have type bigint
).
Additional information about the issue
This does work when using a record instead, however this isn't as useful as I want the descriptor to still be an array so I can iterate over it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment