I was attempting to make this code snippet function properly:
test(a: any[]) {
let b: string[][] = [];
b.push(Object.keys(a[0]));
b.push(...a.map(e => Object.values(e)));
}
However, the compiler is throwing an error for the
b.push(...a.map(e => Object.values)));
line: "TS2345: Argument of type 'unknown[]' is not assignable to parameter of type 'string[]'. Type 'unknown' is not assignable to type 'string'"
Surprisingly, changing the parameter type to test(a: { [s: string]: any }) {
fixes the issue.
My queries:
- Why does it work with the unconventional type?
- What exactly does the peculiar type signify? It seems like a confusing object with keys (possibly of array type) that map to
any
. - When using
a.map(e =>
, without the odd type it infers the type of e ase?: any
, but with the unique signature, it deduces ase: any
. What is the meaning ofe?: any
?