Skip to content

Incorrect excess property error when assigning to intersected array #42715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Artur- opened this issue Feb 9, 2021 · 2 comments · Fixed by #43707
Open

Incorrect excess property error when assigning to intersected array #42715

Artur- opened this issue Feb 9, 2021 · 2 comments · Fixed by #43707
Labels
Bug A bug in TypeScript
Milestone

Comments

@Artur-
Copy link

Artur- commented Feb 9, 2021

Bug Report

🕗 Version & Regression Information

Tested in 3.3.3 and 4.2.0-beta and Nightly

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about type inference

⏯ Playground Link

Playground link with relevant code

💻 Code

export interface ChildrenFn {
    (): Route[];
}

interface Route {
    path: string;
    children?: Route[] | ChildrenFn;
}

type RouteWithTitle = Route & { title?: string; children?: RouteWithTitle[] };

const mainView: RouteWithTitle = {
    path: "",
    children: [
        {
            path: "",
            title: "Empty",
        }
    ],
};

🙁 Actual behavior

Compile error highlighting title: "Empty":

Type '{ path: string; children: { path: string; title: string; }[]; }' is not assignable to type 'RouteWithTitle'.
  Type '{ path: string; children: { path: string; title: string; }[]; }' is not assignable to type 'Route'.
    Types of property 'children' are incompatible.
      Type '{ path: string; title: string; }[]' is not assignable to type 'ChildrenFn | Route[] | undefined'.
        Type '{ path: string; title: string; }[]' is not assignable to type 'Route[]'.
          Type '{ path: string; title: string; }' is not assignable to type 'Route'.
            Object literal may only specify known properties, and 'title' does not exist in type 'Route'.(2322)

🙂 Expected behavior

The type of the object inside children should be RouteWithTitle and no error should occur.

Extracting the child to a const "fixes" the error even though no more type info is given to the compiler:

const c = {
            path: "",
            title: "Empty",
        };

const mainView: RouteWithTitle = {
    path: "",
    children: [
       c
    ],
};

Playground link with the code that works

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Feb 9, 2021
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.3.0 milestone Feb 9, 2021
@RyanCavanaugh RyanCavanaugh changed the title TypeScript is unable to infer correct type in one of two seemingly equal cases Incorrect excess property error when assigning to intersected array Feb 9, 2021
@RyanCavanaugh
Copy link
Member

export interface Other {
    other: string;
}

interface Route {
    path: string;
    children?: Route[] | Other;
}

interface Root {
    path: string;
    children: Route;
}

type RouteWithTitle = Route & {
    children: (Route & { title?: string })[];
};

const mainView: RouteWithTitle = {
    path: "",
    children: [
        {
            path: "",
            title: "Empty" // <- Incorrect error
        }
    ],
};
// Legal
const p = mainView.children[0].title

@sandersn
Copy link
Member

sandersn commented May 6, 2021

I reverted the fix to this because both excess and common property checks are handled by the same flag in assignability, and @types/mongodb relies on common property checks for some correct errors. Decoupling these checks is likely to be extremely complex to verify and therefore not worth it.

@sandersn sandersn modified the milestones: TypeScript 4.3.1, Backlog May 6, 2021
@sandersn sandersn removed their assignment May 6, 2021
@sandersn sandersn removed the Fix Available A PR has been opened for this issue label May 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants