Skip to content

Commit

Permalink
fix: typescript 4.8 type issues #9331 (#9357)
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr authored Sep 15, 2022
1 parent 773a4fe commit a1960e1
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 56 deletions.
19 changes: 9 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
"sqlite3": "^5.0.11",
"ts-node": "^10.7.0",
"typeorm-aurora-data-api-driver": "^2.0.0",
"typescript": "^4.6.2"
"typescript": "^4.8.2"
},
"peerDependencies": {
"@google-cloud/spanner": "^5.18.0",
Expand Down
4 changes: 2 additions & 2 deletions src/data-source/DataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ export class DataSource {
/**
* Creates a new query builder that can be used to build a SQL query.
*/
createQueryBuilder<Entity>(
createQueryBuilder<Entity extends ObjectLiteral>(
entityClass: EntityTarget<Entity>,
alias: string,
queryRunner?: QueryRunner,
Expand All @@ -535,7 +535,7 @@ export class DataSource {
/**
* Creates a new query builder that can be used to build a SQL query.
*/
createQueryBuilder<Entity>(
createQueryBuilder<Entity extends ObjectLiteral>(
entityOrRunner?: EntityTarget<Entity> | QueryRunner,
alias?: string,
queryRunner?: QueryRunner,
Expand Down
44 changes: 22 additions & 22 deletions src/entity-manager/EntityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class EntityManager {
/**
* Creates a new query builder that can be used to build a SQL query.
*/
createQueryBuilder<Entity>(
createQueryBuilder<Entity extends ObjectLiteral>(
entityClass: EntityTarget<Entity>,
alias: string,
queryRunner?: QueryRunner,
Expand All @@ -189,7 +189,7 @@ export class EntityManager {
/**
* Creates a new query builder that can be used to build a SQL query.
*/
createQueryBuilder<Entity>(
createQueryBuilder<Entity extends ObjectLiteral>(
entityClass?: EntityTarget<Entity> | QueryRunner,
alias?: string,
queryRunner?: QueryRunner,
Expand Down Expand Up @@ -659,7 +659,7 @@ export class EntityManager {
* Does not check if entity exist in the database, so query will fail if duplicate entity is being inserted.
* You can execute bulk inserts using this method.
*/
async insert<Entity>(
async insert<Entity extends ObjectLiteral>(
target: EntityTarget<Entity>,
entity:
| QueryDeepPartialEntity<Entity>
Expand All @@ -672,7 +672,7 @@ export class EntityManager {
.execute()
}

async upsert<Entity>(
async upsert<Entity extends ObjectLiteral>(
target: EntityTarget<Entity>,
entityOrEntities:
| QueryDeepPartialEntity<Entity>
Expand Down Expand Up @@ -736,7 +736,7 @@ export class EntityManager {
* Does not check if entity exist in the database.
* Condition(s) cannot be empty.
*/
update<Entity>(
update<Entity extends ObjectLiteral>(
target: EntityTarget<Entity>,
criteria:
| string
Expand Down Expand Up @@ -791,7 +791,7 @@ export class EntityManager {
* Does not check if entity exist in the database.
* Condition(s) cannot be empty.
*/
delete<Entity>(
delete<Entity extends ObjectLiteral>(
targetOrEntity: EntityTarget<Entity>,
criteria:
| string
Expand Down Expand Up @@ -950,7 +950,7 @@ export class EntityManager {
* Counts entities that match given options.
* Useful for pagination.
*/
count<Entity>(
count<Entity extends ObjectLiteral>(
entityClass: EntityTarget<Entity>,
options?: FindManyOptions<Entity>,
): Promise<number> {
Expand All @@ -968,7 +968,7 @@ export class EntityManager {
* Counts entities that match given conditions.
* Useful for pagination.
*/
countBy<Entity>(
countBy<Entity extends ObjectLiteral>(
entityClass: EntityTarget<Entity>,
where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[],
): Promise<number> {
Expand All @@ -981,7 +981,7 @@ export class EntityManager {
/**
* Finds entities that match given find options.
*/
async find<Entity>(
async find<Entity extends ObjectLiteral>(
entityClass: EntityTarget<Entity>,
options?: FindManyOptions<Entity>,
): Promise<Entity[]> {
Expand All @@ -998,7 +998,7 @@ export class EntityManager {
/**
* Finds entities that match given find options.
*/
async findBy<Entity>(
async findBy<Entity extends ObjectLiteral>(
entityClass: EntityTarget<Entity>,
where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[],
): Promise<Entity[]> {
Expand All @@ -1016,7 +1016,7 @@ export class EntityManager {
* Also counts all entities that match given conditions,
* but ignores pagination settings (from and take options).
*/
findAndCount<Entity>(
findAndCount<Entity extends ObjectLiteral>(
entityClass: EntityTarget<Entity>,
options?: FindManyOptions<Entity>,
): Promise<[Entity[], number]> {
Expand All @@ -1035,7 +1035,7 @@ export class EntityManager {
* Also counts all entities that match given conditions,
* but ignores pagination settings (from and take options).
*/
findAndCountBy<Entity>(
findAndCountBy<Entity extends ObjectLiteral>(
entityClass: EntityTarget<Entity>,
where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[],
): Promise<[Entity[], number]> {
Expand All @@ -1058,7 +1058,7 @@ export class EntityManager {
* id: In([1, 2, 3])
* })
*/
async findByIds<Entity>(
async findByIds<Entity extends ObjectLiteral>(
entityClass: EntityTarget<Entity>,
ids: any[],
): Promise<Entity[]> {
Expand All @@ -1078,7 +1078,7 @@ export class EntityManager {
* Finds first entity by a given find options.
* If entity was not found in the database - returns null.
*/
async findOne<Entity>(
async findOne<Entity extends ObjectLiteral>(
entityClass: EntityTarget<Entity>,
options: FindOneOptions<Entity>,
): Promise<Entity | null> {
Expand Down Expand Up @@ -1109,7 +1109,7 @@ export class EntityManager {
* Finds first entity that matches given where condition.
* If entity was not found in the database - returns null.
*/
async findOneBy<Entity>(
async findOneBy<Entity extends ObjectLiteral>(
entityClass: EntityTarget<Entity>,
where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[],
): Promise<Entity | null> {
Expand All @@ -1134,7 +1134,7 @@ export class EntityManager {
* id: 1 // where "id" is your primary column name
* })
*/
async findOneById<Entity>(
async findOneById<Entity extends ObjectLiteral>(
entityClass: EntityTarget<Entity>,
id: number | string | Date | ObjectID,
): Promise<Entity | null> {
Expand All @@ -1153,7 +1153,7 @@ export class EntityManager {
* Finds first entity by a given find options.
* If entity was not found in the database - rejects with error.
*/
async findOneOrFail<Entity>(
async findOneOrFail<Entity extends ObjectLiteral>(
entityClass: EntityTarget<Entity>,
options: FindOneOptions<Entity>,
): Promise<Entity> {
Expand All @@ -1173,7 +1173,7 @@ export class EntityManager {
* Finds first entity that matches given where condition.
* If entity was not found in the database - rejects with error.
*/
async findOneByOrFail<Entity>(
async findOneByOrFail<Entity extends ObjectLiteral>(
entityClass: EntityTarget<Entity>,
where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[],
): Promise<Entity> {
Expand Down Expand Up @@ -1209,7 +1209,7 @@ export class EntityManager {
/**
* Increments some column by provided value of the entities matched given conditions.
*/
async increment<Entity>(
async increment<Entity extends ObjectLiteral>(
entityClass: EntityTarget<Entity>,
conditions: any,
propertyPath: string,
Expand Down Expand Up @@ -1246,7 +1246,7 @@ export class EntityManager {
/**
* Decrements some column by provided value of the entities matched given conditions.
*/
async decrement<Entity>(
async decrement<Entity extends ObjectLiteral>(
entityClass: EntityTarget<Entity>,
conditions: any,
propertyPath: string,
Expand Down Expand Up @@ -1354,8 +1354,8 @@ export class EntityManager {
* sets current EntityManager instance to it. Used to work with custom repositories
* in transactions.
*/
withRepository<Entity extends ObjectLiteral, R extends Repository<Entity>>(
repository: R,
withRepository<Entity extends ObjectLiteral, R extends Repository<any>>(
repository: R & Repository<Entity>,
): R {
const repositoryConstructor =
repository.constructor as typeof Repository
Expand Down
3 changes: 2 additions & 1 deletion src/find-options/FindOptionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { FindRelationsNotFoundError } from "../error/FindRelationsNotFoundError"
import { EntityMetadata } from "../metadata/EntityMetadata"
import { DriverUtils } from "../driver/DriverUtils"
import { FindTreeOptions } from "./FindTreeOptions"
import { ObjectLiteral } from "../common/ObjectLiteral"

/**
* Utilities to work with FindOptions.
Expand Down Expand Up @@ -239,7 +240,7 @@ export class FindOptionsUtils {
return qb;
}*/

static applyOptionsToTreeQueryBuilder<T>(
static applyOptionsToTreeQueryBuilder<T extends ObjectLiteral>(
qb: SelectQueryBuilder<T>,
options?: FindTreeOptions,
): SelectQueryBuilder<T> {
Expand Down
4 changes: 2 additions & 2 deletions src/query-builder/DeleteQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { InstanceChecker } from "../util/InstanceChecker"
/**
* Allows to build complex sql queries in a fashion way and execute those queries.
*/
export class DeleteQueryBuilder<Entity>
export class DeleteQueryBuilder<Entity extends ObjectLiteral>
extends QueryBuilder<Entity>
implements WhereExpressionBuilder
{
Expand Down Expand Up @@ -116,7 +116,7 @@ export class DeleteQueryBuilder<Entity>
* Specifies FROM which entity's table select/update/delete will be executed.
* Also sets a main string alias of the selection data.
*/
from<T>(
from<T extends ObjectLiteral>(
entityTarget: EntityTarget<T>,
aliasName?: string,
): DeleteQueryBuilder<T> {
Expand Down
6 changes: 4 additions & 2 deletions src/query-builder/InsertQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import { InstanceChecker } from "../util/InstanceChecker"
/**
* Allows to build complex sql queries in a fashion way and execute those queries.
*/
export class InsertQueryBuilder<Entity> extends QueryBuilder<Entity> {
export class InsertQueryBuilder<
Entity extends ObjectLiteral,
> extends QueryBuilder<Entity> {
readonly "@instanceof" = Symbol.for("InsertQueryBuilder")

// -------------------------------------------------------------------------
Expand Down Expand Up @@ -227,7 +229,7 @@ export class InsertQueryBuilder<Entity> extends QueryBuilder<Entity> {
/**
* Specifies INTO which entity's table insertion will be executed.
*/
into<T>(
into<T extends ObjectLiteral>(
entityTarget: EntityTarget<T>,
columns?: string[],
): InsertQueryBuilder<T> {
Expand Down
6 changes: 3 additions & 3 deletions src/query-builder/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { escapeRegExp } from "../util/escapeRegExp"
/**
* Allows to build complex sql queries in a fashion way and execute those queries.
*/
export abstract class QueryBuilder<Entity> {
export abstract class QueryBuilder<Entity extends ObjectLiteral> {
readonly "@instanceof" = Symbol.for("QueryBuilder")

// -------------------------------------------------------------------------
Expand Down Expand Up @@ -216,7 +216,7 @@ export abstract class QueryBuilder<Entity> {
/**
* Creates UPDATE query for the given entity and applies given update values.
*/
update<Entity>(
update<Entity extends ObjectLiteral>(
entity: EntityTarget<Entity>,
updateSet?: QueryDeepPartialEntity<Entity>,
): UpdateQueryBuilder<Entity>
Expand Down Expand Up @@ -308,7 +308,7 @@ export abstract class QueryBuilder<Entity> {
/**
* Sets entity's relation with which this query builder gonna work.
*/
relation<T>(
relation<T extends ObjectLiteral>(
entityTarget: EntityTarget<T>,
propertyPath: string,
): RelationQueryBuilder<T>
Expand Down
14 changes: 10 additions & 4 deletions src/query-builder/QueryPartialEntity.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ObjectLiteral } from "../common/ObjectLiteral"

/**
* Make all properties in T optional
*/
Expand All @@ -8,12 +10,16 @@ export type QueryPartialEntity<T> = {
/**
* Make all properties in T optional. Deep version.
*/
export type QueryDeepPartialEntity<T> = {
export type QueryDeepPartialEntity<T> = _QueryDeepPartialEntity<
ObjectLiteral extends T ? unknown : T
>

type _QueryDeepPartialEntity<T> = {
[P in keyof T]?:
| (T[P] extends Array<infer U>
? Array<QueryDeepPartialEntity<U>>
? Array<_QueryDeepPartialEntity<U>>
: T[P] extends ReadonlyArray<infer U>
? ReadonlyArray<QueryDeepPartialEntity<U>>
: QueryDeepPartialEntity<T[P]>)
? ReadonlyArray<_QueryDeepPartialEntity<U>>
: _QueryDeepPartialEntity<T[P]>)
| (() => string)
}
5 changes: 4 additions & 1 deletion src/query-builder/RelationQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import { RelationUpdater } from "./RelationUpdater"
import { RelationRemover } from "./RelationRemover"
import { TypeORMError } from "../error"
import { ObjectUtils } from "../util/ObjectUtils"
import { ObjectLiteral } from "../common/ObjectLiteral"

/**
* Allows to work with entity relations and perform specific operations with those relations.
*
* todo: add transactions everywhere
*/
export class RelationQueryBuilder<Entity> extends QueryBuilder<Entity> {
export class RelationQueryBuilder<
Entity extends ObjectLiteral,
> extends QueryBuilder<Entity> {
readonly "@instanceof" = Symbol.for("RelationQueryBuilder")

// -------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit a1960e1

Please sign in to comment.