Repository: typeorm/typeorm
File: src/query-builder/SelectQueryBuilder.ts (TypeScript)

Code before:
        if (this.expressionMap.lockMode === "optimistic")
            throw new OptimisticLockCanNotBeUsedError()

        const queryRunner = this.obtainQueryRunner()
        let transactionStartedByUs: boolean = false
        try {
            // start transaction if it was enabled
            if (
                this.expressionMap.useTransaction === true &&
                queryRunner.isTransactionActive === false
            ) {
                await queryRunner.startTransaction()
                transactionStartedByUs = true
            }

            this.expressionMap.queryEntity = true
            const entitiesAndRaw = await this.executeEntitiesAndRawResults(
                queryRunner,
            )
            this.expressionMap.queryEntity = false

            let count
            let lazyCount = this.lazyCount(entitiesAndRaw)
            if (lazyCount !== undefined) {
                count = lazyCount
            } else {
                const cacheId = this.expressionMap.cacheId
                // Creates a new cacheId for the count query, or it will retrieve the above query results
                // and count will return 0.
                if (cacheId) {
                    this.expressionMap.cacheId = `${cacheId}-count`
                }
                count = await this.executeCountQuery(queryRunner)
            }
            const results: [Entity[], number] = [entitiesAndRaw.entities, count]

            // close transaction if we started it
            if (transactionStartedByUs) {
                await queryRunner.commitTransaction()
            }

            return results
        } catch (error) {
            // rollback transaction if we started it
            if (transactionStartedByUs) {
                try {
                    await queryRunner.rollbackTransaction()
                } catch (rollbackError) {}
            }
            throw error
        } finally {

Review comment (refactor):
These variable declarations should be typed. And `lazyCount` should be a const since it's never updated.

In fact, we can simplify and lose the `lazyCount` variable entirely:

```suggestion
            let count: number | undefined = this.lazyCount(entitiesAndRaw)
            if (count === undefined) {
```

Code after:
        if (this.expressionMap.lockMode === "optimistic")
            throw new OptimisticLockCanNotBeUsedError()

        const queryRunner = this.obtainQueryRunner()
        let transactionStartedByUs: boolean = false
        try {
            // start transaction if it was enabled
            if (
                this.expressionMap.useTransaction === true &&
                queryRunner.isTransactionActive === false
            ) {
                await queryRunner.startTransaction()
                transactionStartedByUs = true
            }

            this.expressionMap.queryEntity = true
            const entitiesAndRaw = await this.executeEntitiesAndRawResults(
                queryRunner,
            )
            this.expressionMap.queryEntity = false

            let count: number | undefined = this.lazyCount(entitiesAndRaw)
            if (count === undefined) {
                const cacheId = this.expressionMap.cacheId
                // Creates a new cacheId for the count query, or it will retrieve the above query results
                // and count will return 0.
                if (cacheId) {
                    this.expressionMap.cacheId = `${cacheId}-count`
                }
                count = await this.executeCountQuery(queryRunner)
            }
            const results: [Entity[], number] = [entitiesAndRaw.entities, count]

            // close transaction if we started it
            if (transactionStartedByUs) {
                await queryRunner.commitTransaction()
            }

            return results
        } catch (error) {
            // rollback transaction if we started it
            if (transactionStartedByUs) {
                try {
                    await queryRunner.rollbackTransaction()
                } catch (rollbackError) {}
            }
            throw error
        } finally {
            if (queryRunner !== this.queryRunner)
                // means we created our own query runner
                await queryRunner.release()