on-internet
← Community

Using a constructor as parameter to Array.map

Stack Overflow ·

I need to map an array of objects to an array of other objects. Here is an example: class A {} class B { constructor(a: A) { // ...do something } } let a_arr: A[] = []; // now I want to create a B[] from the A[] If I use an anonymous function, it compiles without error: let b_arr: B[] = a_arr.map((item) => new B(item)); But that anonymous function seems useless—I suppose we can directly use the constructor: let b_arr: B[] = a_arr.map(B.constructor); Unfortunately, the above line didn't pass compilation: sh$ tsc t.ts t.ts:11:28 - error TS2345: Argument of type 'Function' is not assignable to pa

Comments

Sign in or sign up to comment and upvote.

No comments yet.