I have some JSON data that I've strongly typed with an as const assertion.
const myList = [
{ type: 'uint256'},
{ type: 'address'}
] as const;
Now I want to convert this to the following tuple type:
[number, string]
Basically, if the type is "address" it should resolve to string.
If the type is a "uint256", it should resolve to number.
I intend to use this type to create arrays of data that conform to this schema, eg:
const foo: ConvertToTuple<typeof myList> = [1, 'asdf'];
I'm struggling to write a generic that can do this. Here's where I'm at so far:
type ConvertToTuple<
DataObjects extends readonly { type: "address" | "uint256" }[]
> = DataObjects extends readonly [infer CurrentObject, ...infer RestObjects]
? CurrentObject["type"] extends "address"
? [string, ...ConvertToTuple<RestObjects>]
: CurrentObject["type"] extends "uint256"
? [number, ...ConvertToTuple<RestObject>]
: never
: [];
I don't think I'm using infer correctly here. Typescript is throwing a couple of errors:
Type '"type"' cannot be used to index type 'CurrentObject'
and
Type 'RestObjects' does not satisfy the constraint 'readonly { type: "address" | "uint256"; }[]'.
Type 'unknown[]' is not assignable to type 'readonly { type: "address" | "uint256"; }[]'.
Type 'unknown' is not assignable to type '{ type: "address" | "uint256"; }'.
Any help untangling this is much appreciated!