Closed
Description
TS version 1.8.7
interface Parent {
children: Array<Child>
}
interface Child {
type: 'child';
}
// the line below fails
const p1: Parent[] = [1].map(x => ({ children: [{ type: 'child'}] }))
// the line below succeeds
const p2: Parent = ({ children: [{ type: 'child'}] });
Error:
main.ts(66,7): error TS2322: Type '{ children: { type: string; }[]; }[]' is not assignable to type 'Parent[]'.
Type '{ children: { type: string; }[]; }' is not assignable to type 'Parent'.
Types of property 'children' are incompatible.
Type '{ type: string; }[]' is not assignable to type 'Child[]'.
Type '{ type: string; }' is not assignable to type 'Child'.
Types of property 'type' are incompatible.
Type 'string' is not assignable to type '"child"'.
How come p1
fails but p2
succeeds?