Skip to content

Tsgo considers wrong API usage as correct - difference in typechecking #2889

@jansedlon

Description

@jansedlon

Steps to reproduce

  1. Clone repo https://github.com/jansedlon/tsgo-repro
  2. Install dependencies
  3. Open index.ts

Behavior with typescript@5.9

The constant posts has a type of

const posts: {
    id: string;
    createdAt: Date;
    updatedAt: Date;
}[]

And typescript reports error in attachments object because selecting database fields needs to be inside of columns object and orderBy in attachments like so

attachments: {
  columns: {
    id: true,
    // ...
  },
  orderBy(fields, { asc }) {
    return asc(fields.createdAt);
  }
}

Behavior with tsgo

Not only it doesn't report any error, it even considers wrong API usage as correct and infers types. This would result in runtime error.

The type of posts is inferred as

const posts: {
  attachments: {
    createdAt: Date;
    filename: string | null;
    id: string;
    location: string;
    postId: string | null;
    size: number | null;
    type: string;
    updatedAt: Date;
  }[];
  id: string;
}[]

Note

I have found out that the thing that breaks this is the orderBy function. If you comment that out, it will start reporting errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions