1

Supose I have next funcion:

function cast<T>(v: any): T {
    return v as T;
}

Now, I have an array what I want to cast using map:

const arr = [1, 3, 5];

// works
arr.map(value => cast<number>(value));

// but I want
arr.map(cast<number>);

Is it possible? Can I pass generic on point-free map?

1

3 Answers 3

5

Actually, the method map from the Array prototype is generic.

Array<T>.map<U>(callback: (value: T, index: number, array: T[]) => U): U[]

So, by using your cast function with any type argument (like using it as a pointer-free) returns unknown. Totally expected.

You can cast your array like so

const arr = [1, 3, 5];
const re = arr.map<string>(cast);

Here is an interesting post about this.


I was reading this post months later and another idea just flashed in my mind.
Another solution could be a function wrapper to pass the type argument to the cast function explicitly.

type Fn<T> = (element: any) => T;

const cast = <T>(element: any): T => {
    return element as T;
}

const caster = <T>() => {
    return cast as Fn<T>;
}

const dut: any[] = [1, 2, 3, 4, 5, 6];
const casted = dut.map(caster<number>());

// casted is of type number[]

Although the main solution is still the best, this approach can solve some situations where the target API does not offer any overload to infer the type.

Hope it helps.

0

What you want is not possible as cast<number> is not considered as a callback function which is required by array map.

You can try to see what is and isn’t a function by simply logging them in console.

Try console.log(cast) And console.log(cast<number>)

0

A bit more type-safe alternative (Fail-Fast run-time checks) using point-free style:

function assert(expr: unknown, msg = ""): asserts expr { if (!expr) throw Error(msg); }
const every = <T>(cb: (t: unknown) => t is T) => (a: unknown[]): a is T[] => arr.every(cb);
const isNumber = (mn: unknown): mn is number => typeof mn === "number"

const arr: unknown[] = ... // given some unknown array
assert(every(isNumber)(arr));
arr // arr now of type number[] (or has thrown, which could be fetched by an error handler)

Playground sample

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.