Skip to content
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

"Order" not works when custom toString() is defined #7178

Closed
2 of 21 tasks
rysi3k opened this issue Dec 11, 2020 · 1 comment · Fixed by #7188, mattwelke/typeorm-postgres-example#165 or newerton/gobarber-2-backend#17
Closed
2 of 21 tasks

Comments

@rysi3k
Copy link
Contributor

rysi3k commented Dec 11, 2020

Issue Description

When model has custom toString() implementation it is not possible to proper use order clause.
Additional typeorm thinks that is ok to sort by method name.

Expected Behavior

  1. Sorting when toString() is defined should be possible
  2. Not possible to sort by defined methods

Actual Behavior

TS2769: No overload matches this call.
  Overload 1 of 2, '(options?: FindManyOptions<Transaction>): Promise<Transaction[]>', gave the following error.
    Type '{ someMethod: "ASC"; }' is not assignable to type '{ id?: 1 | "ASC" | "DESC" | -1; name?: 1 | "ASC" | "DESC" | -1; toString?: 1 | "ASC" | "DESC" | -1; someMethod?: 1 | "ASC" | "DESC" | -1; }'.
      Types of property 'toString' are incompatible.
        Type '() => string' is not assignable to type '1 | "ASC" | "DESC" | -1'.
          Type '() => string' is not assignable to type '-1'.
  Overload 2 of 2, '(conditions?: FindConditions<Transaction>): Promise<Transaction[]>', gave the following error.
    Argument of type '{ order: { someMethod: string; }; where: { id: number; }; }' is not assignable to parameter of type 'FindConditions<Transaction>'.
      Object literal may only specify known properties, and 'order' does not exist in type 'FindConditions<Transaction>'.

Steps to Reproduce

import {Column, Entity, EntityRepository, PrimaryGeneratedColumn, Repository} from 'typeorm';

@Entity('Transactions')
class Transaction {
    @PrimaryGeneratedColumn()
    public id?: number;

    @Column()
    public name?: string;

    // when defined there is ERROR in find call, below
    public toString() {
        return `${this.name}`;
    }

    public someMethod() {
        return 123;
    }
}

@EntityRepository(Transaction)
class TransactionsRepository extends Repository<Transaction> {

}

const repo = new TransactionsRepository();
repo.find({
    order: {
        someMethod: 'ASC', // wrong usage, but for typeorm it is ok
    },
    where: {
        id: 1,
    },
});

My Environment

Dependency Version
Operating System macos
Node.js version v12.18.0
Typescript version v3.9.7
TypeORM version v0.2.29

Relevant Database Driver(s)

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don't know how to start. I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.
@nebkat
Copy link
Contributor

nebkat commented Dec 12, 2020

Interesting problem, I guess the solution is to exclude all function keys since you can't sort by functions anyway?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment