Skip to content

Compile error using interface with string literal type inside function #8131

Closed
@OliverJAsh

Description

@OliverJAsh

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions