Skip to content

Commit

Permalink
fix: generic constraint relaxing
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Apr 1, 2022
1 parent f990b3b commit 05bd89f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 37 deletions.
30 changes: 15 additions & 15 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 @@ -72,7 +72,7 @@
"source-map-support": "^0.5.21",
"standard-version": "^9.3.2",
"ts-node": "^10.7.0",
"tsd": "^0.19.1",
"tsd": "^0.20.0",
"typescript": "^4.6.3",
"typescript-cached-transpile": "^0.0.6",
"xml2js": "^0.4.23",
Expand Down
16 changes: 6 additions & 10 deletions src/cursor/abstract_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export type AbstractCursorEvents = {

/** @public */
export abstract class AbstractCursor<
TSchema extends Document = any,
TSchema = any,
CursorEvents extends AbstractCursorEvents = AbstractCursorEvents
> extends TypedEventEmitter<CursorEvents> {
/** @internal */
Expand All @@ -118,7 +118,7 @@ export abstract class AbstractCursor<
/** @internal */
[kTopology]: Topology;
/** @internal */
[kTransform]?: (doc: TSchema) => Document;
[kTransform]?: (doc: TSchema) => any;
/** @internal */
[kInitialized]: boolean;
/** @internal */
Expand Down Expand Up @@ -477,7 +477,7 @@ export abstract class AbstractCursor<
* ```
* @param transform - The mapping transformation method.
*/
map<T extends Document = any>(transform: (doc: TSchema) => T): AbstractCursor<T> {
map<T = any>(transform: (doc: TSchema) => T): AbstractCursor<T> {
assertUninitialized(this);
const oldTransform = this[kTransform] as (doc: TSchema) => TSchema; // TODO(NODE-3283): Improve transform typing
if (oldTransform) {
Expand Down Expand Up @@ -622,7 +622,7 @@ export abstract class AbstractCursor<
}
}

function nextDocument<T extends Document>(cursor: AbstractCursor): T | null {
function nextDocument<T>(cursor: AbstractCursor): T | null {
if (cursor[kDocuments] == null || !cursor[kDocuments].length) {
return null;
}
Expand All @@ -640,11 +640,7 @@ function nextDocument<T extends Document>(cursor: AbstractCursor): T | null {
return null;
}

function next<T extends Document>(
cursor: AbstractCursor,
blocking: boolean,
callback: Callback<T | null>
): void {
function next<T>(cursor: AbstractCursor<T>, blocking: boolean, callback: Callback<T | null>): void {
const cursorId = cursor[kId];
if (cursor.closed) {
return callback(undefined, null);
Expand Down Expand Up @@ -823,7 +819,7 @@ export function assertUninitialized(cursor: AbstractCursor): void {
}
}

function makeCursorStream<TSchema extends Document>(cursor: AbstractCursor<TSchema>) {
function makeCursorStream(cursor: AbstractCursor) {
const readable = new Readable({
objectMode: true,
autoDestroy: false,
Expand Down
8 changes: 3 additions & 5 deletions src/cursor/aggregation_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ const kOptions = Symbol('options');
* or higher stream
* @public
*/
export class AggregationCursor<
TSchema extends Document = Document
> extends AbstractCursor<TSchema> {
export class AggregationCursor<TSchema = any> extends AbstractCursor<TSchema> {
/** @internal */
[kPipeline]: Document[];
/** @internal */
Expand Down Expand Up @@ -58,7 +56,7 @@ export class AggregationCursor<
});
}

override map<T extends Document>(transform: (doc: TSchema) => T): AggregationCursor<T> {
override map<T>(transform: (doc: TSchema) => T): AggregationCursor<T> {
return super.map(transform) as AggregationCursor<T>;
}

Expand Down Expand Up @@ -101,7 +99,7 @@ export class AggregationCursor<
}

/** Add a group stage to the aggregation pipeline */
group<T extends Document = TSchema>($group: Document): AggregationCursor<T>;
group<T = TSchema>($group: Document): AggregationCursor<T>;
group($group: Document): this {
assertUninitialized(this);
this[kPipeline].push({ $group });
Expand Down
4 changes: 2 additions & 2 deletions src/cursor/find_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const FLAGS = [
] as const;

/** @public */
export class FindCursor<TSchema extends Document = Document> extends AbstractCursor<TSchema> {
export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
/** @internal */
[kFilter]: Document;
/** @internal */
Expand Down Expand Up @@ -63,7 +63,7 @@ export class FindCursor<TSchema extends Document = Document> extends AbstractCur
});
}

override map<T extends Document>(transform: (doc: TSchema) => T): FindCursor<T> {
override map<T>(transform: (doc: TSchema) => T): FindCursor<T> {
return super.map(transform) as FindCursor<T>;
}

Expand Down
10 changes: 6 additions & 4 deletions test/types/community/collection/insertX.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ expectError(
);

// defined _id's that are not of type ObjectId cannot be cast to ObjectId
const collectionWithRequiredNumericId =
db.collection<{ _id: number; otherField: string }>('testCollection');
const collectionWithRequiredNumericId = db.collection<{ _id: number; otherField: string }>(
'testCollection'
);

expectError(
collectionWithRequiredNumericId.insertOne({
Expand All @@ -143,8 +144,9 @@ expectError(
})
);

const collectionWithOptionalNumericId =
db.collection<{ _id?: number; otherField: string }>('testCollection');
const collectionWithOptionalNumericId = db.collection<{ _id?: number; otherField: string }>(
'testCollection'
);

expectError(
collectionWithOptionalNumericId.insertOne({
Expand Down

0 comments on commit 05bd89f

Please sign in to comment.