#How do I forward the tupleness to another type-argument? (two array types of the same length)
11 messages ยท Page 1 of 1 (latest)
Preview:```ts
export interface MappedArray<MappedTo> {
[index: number]: MappedTo
values: () => readonly MappedTo[]
length: number
Symbol.iterator: Iterator<MappedTo>
}
export function map<Element, MapTo>(options: {
data: () => readonly El
...```
You can choose specific lines to embed by selecting them before copying the link.
@nova sail the issue is that it's impossible for the return type of map.data to be a tuple...
since you explicitly define it to be an array
how do I explicitly define it to be a tuple?
!ts
export interface MappedArray<Arr extends readonly unknown[], MappedTo> {
[index: number]: MappedTo;
values: () => { [K in keyof Arr]: MappedTo };
length: number;
[Symbol.iterator](): Iterator<MappedTo>;
}
export function map<Arr extends readonly unknown[], MapTo>(
options: {
data: () => Arr;
map: (element: Arr[number]) => MapTo;
}
) {
/**
actual implementation omitted
*/
return options.data as unknown as MappedArray<Arr, MapTo>;
}
function doubleEach<T extends readonly number[]>(input: T): T {
return input.map(i => i * 2) as unknown as T;
}
function doubleEachFn<T extends readonly number[]>(input: () => T): T {
return input().map(i => i * 2) as unknown as T;
}
function doubleEachFn2<T, Arr extends readonly T[]>(input: () => Arr): Arr {
// implementation not important
return input().map((i: any) => i * 2) as unknown as Arr;
}
const original = [1, 2, 3, 4] as const;
let result: number = doubleEach(original)[0];
let result2: number = doubleEachFn(() => original)[0];
let result3: number = doubleEachFn2(() => original)[0];
let result4: number = map({ data: () => original, map: (num) => num * 2 }).values()[0];
๐ค
I am unsure of what you intended with your original code, could you provide what you expected to get back on your result variables from your function calls?
I figured it out, actually ๐