打字稿通用方法类型注释
typescript generic method type annotation
在以下方法中,我遇到了一个问题,即我无法在不出现错误的情况下找到 changes 参数的类型。这是一个普通的 nestjs 服务,它是通用的,应该涵盖标准的 crud 功能。但是在更新功能中,我没有找到更改的通用类型。
如果我可以使用 Model T 类型对我来说是合乎逻辑的,不幸的是猫鼬不允许。
开放想法,提前致谢。
async updateOne(
conditions: Partial<Record<keyof T, unknown>>,
changes: any, // type for cahnges
// projection: string | Record<string, unknown> = {},
// options: Record<string, unknown> = {},
): Promise<T> {
try {
return await this.model.findOneAndUpdate(
conditions as FilterQuery<T>,
changes,
);
} catch (err) {
console.log('generic service error: ' + err);
throw new InternalServerErrorException();
}
}
这里是完整的 CRUD 服务 class
import { Injectable, InternalServerErrorException } from '@nestjs/common';
import { Document, FilterQuery, Model } from 'mongoose';
@Injectable()
export abstract class GenericCrudService<T extends Document> {
constructor(private readonly model: Model<T>) {}
async findAll(
conditions: Partial<Record<keyof T, unknown>>,
projection: string | Record<string, unknown> = {},
options: Record<string, unknown> = {},
): Promise<T[]> {
try {
return await this.model.find(
conditions as FilterQuery<T>,
projection,
options,
);
} catch (err) {
console.log('generic error: ' + err);
throw new InternalServerErrorException();
}
}
async updateOne(
conditions: Partial<Record<keyof T, unknown>>,
changes: any,
// projection: string | Record<string, unknown> = {},
// options: Record<string, unknown> = {},
): Promise<T> {
try {
return await this.model.findOneAndUpdate(
conditions as FilterQuery<T>,
changes,
);
} catch (err) {
console.log('generic service error: ' + err);
throw new InternalServerErrorException();
}
}
}
这是 mongoose 的 updateOne
typedef:
updateOne(update?: UpdateQuery<this> | UpdateWithAggregationPipeline, options?: QueryOptions | null, callback?: Callback): Query<any, this>;
他们引用了一个类型UpdateQuery<T>
。这就是您要找的吗?
在以下方法中,我遇到了一个问题,即我无法在不出现错误的情况下找到 changes 参数的类型。这是一个普通的 nestjs 服务,它是通用的,应该涵盖标准的 crud 功能。但是在更新功能中,我没有找到更改的通用类型。
如果我可以使用 Model T 类型对我来说是合乎逻辑的,不幸的是猫鼬不允许。
开放想法,提前致谢。
async updateOne(
conditions: Partial<Record<keyof T, unknown>>,
changes: any, // type for cahnges
// projection: string | Record<string, unknown> = {},
// options: Record<string, unknown> = {},
): Promise<T> {
try {
return await this.model.findOneAndUpdate(
conditions as FilterQuery<T>,
changes,
);
} catch (err) {
console.log('generic service error: ' + err);
throw new InternalServerErrorException();
}
}
这里是完整的 CRUD 服务 class
import { Injectable, InternalServerErrorException } from '@nestjs/common';
import { Document, FilterQuery, Model } from 'mongoose';
@Injectable()
export abstract class GenericCrudService<T extends Document> {
constructor(private readonly model: Model<T>) {}
async findAll(
conditions: Partial<Record<keyof T, unknown>>,
projection: string | Record<string, unknown> = {},
options: Record<string, unknown> = {},
): Promise<T[]> {
try {
return await this.model.find(
conditions as FilterQuery<T>,
projection,
options,
);
} catch (err) {
console.log('generic error: ' + err);
throw new InternalServerErrorException();
}
}
async updateOne(
conditions: Partial<Record<keyof T, unknown>>,
changes: any,
// projection: string | Record<string, unknown> = {},
// options: Record<string, unknown> = {},
): Promise<T> {
try {
return await this.model.findOneAndUpdate(
conditions as FilterQuery<T>,
changes,
);
} catch (err) {
console.log('generic service error: ' + err);
throw new InternalServerErrorException();
}
}
}
这是 mongoose 的 updateOne
typedef:
updateOne(update?: UpdateQuery<this> | UpdateWithAggregationPipeline, options?: QueryOptions | null, callback?: Callback): Query<any, this>;
他们引用了一个类型UpdateQuery<T>
。这就是您要找的吗?