From 747cdb76afe691dc6721378428426673bcc8aead Mon Sep 17 00:00:00 2001 From: bangbang93 Date: Fri, 3 Dec 2021 12:46:23 +0800 Subject: [PATCH] fix type --- package-lock.json | 43 ++++++++++++++++++++++++++++--------------- package.json | 4 ++-- src/schema.ts | 35 ++++++++++++++++++----------------- 3 files changed, 48 insertions(+), 34 deletions(-) diff --git a/package-lock.json b/package-lock.json index e95d42f..c546333 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "2.1.3", "license": "MIT", "dependencies": { - "@sindresorhus/is": "^3.0.0", + "@sindresorhus/is": "^4.2.0", "reflect-metadata": "^0.1.12", "type-fest": "^2.8.0" }, @@ -22,7 +22,7 @@ "should": "^13.2.3", "ts-node": "^8.10.2", "typedoc": "^0.17.3", - "typescript": "^3.9.7" + "typescript": "^4.5.2" }, "peerDependencies": { "mongoose": "^6.0.14" @@ -66,9 +66,15 @@ } }, "node_modules/@sindresorhus/is": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-3.0.0.tgz", - "integrity": "sha512-kqA5I6Yun7PBHk8WN9BBP1c7FfN2SrD05GuVSEYPqDb4nerv7HqYfgBfMIKmT/EuejURkJKLZuLyGKGs6WEG9w==" + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-4.2.0.tgz", + "integrity": "sha512-VkE3KLBmJwcCaVARtQpfuKcKv8gcBmUubrfHGF84dXuuW6jgsRYxPtzcIhPyK9WAPpRt2/xY6zkD9MnRaJzSyw==", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sindresorhus/is?sponsor=1" + } }, "node_modules/@types/color-name": { "version": "1.1.1", @@ -2074,10 +2080,17 @@ } }, "node_modules/typescript": { - "version": "3.9.7", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.7.tgz", - "integrity": "sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw==", - "dev": true + "version": "4.5.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.5.2.tgz", + "integrity": "sha512-5BlMof9H1yGt0P8/WF+wPNw6GfctgGjXp5hkblpyT+8rkASSmkUKMXrxR0Xg8ThVCi/JnHQiKXeBaEwCeQwMFw==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=4.2.0" + } }, "node_modules/uglify-js": { "version": "3.8.1", @@ -2356,9 +2369,9 @@ } }, "@sindresorhus/is": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-3.0.0.tgz", - "integrity": "sha512-kqA5I6Yun7PBHk8WN9BBP1c7FfN2SrD05GuVSEYPqDb4nerv7HqYfgBfMIKmT/EuejURkJKLZuLyGKGs6WEG9w==" + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-4.2.0.tgz", + "integrity": "sha512-VkE3KLBmJwcCaVARtQpfuKcKv8gcBmUubrfHGF84dXuuW6jgsRYxPtzcIhPyK9WAPpRt2/xY6zkD9MnRaJzSyw==" }, "@types/color-name": { "version": "1.1.1", @@ -4300,9 +4313,9 @@ } }, "typescript": { - "version": "3.9.7", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.7.tgz", - "integrity": "sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw==", + "version": "4.5.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.5.2.tgz", + "integrity": "sha512-5BlMof9H1yGt0P8/WF+wPNw6GfctgGjXp5hkblpyT+8rkASSmkUKMXrxR0Xg8ThVCi/JnHQiKXeBaEwCeQwMFw==", "dev": true }, "uglify-js": { diff --git a/package.json b/package.json index 5e2328a..3f99e7b 100644 --- a/package.json +++ b/package.json @@ -38,10 +38,10 @@ "should": "^13.2.3", "ts-node": "^8.10.2", "typedoc": "^0.17.3", - "typescript": "^3.9.7" + "typescript": "^4.5.2" }, "dependencies": { - "@sindresorhus/is": "^3.0.0", + "@sindresorhus/is": "^4.2.0", "reflect-metadata": "^0.1.12", "type-fest": "^2.8.0" }, diff --git a/src/schema.ts b/src/schema.ts index 615ac9c..3018c7c 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -1,13 +1,14 @@ import {Schema, SchemaDefinition, SchemaTypeOptions, Types} from 'mongoose' +import {Constructor} from 'type-fest' import {getSchema, validators} from './index' -import {Constructor, Fn, getMongooseMeta, IMongooseClass, mongooseMeta, Prototype} from './meta' +import {Fn, getMongooseMeta, IMongooseClass, mongooseMeta} from './meta' import {getType} from './util' type SchemaType = Omit, 'type'> & {type?: T} export function prop(options: SchemaType = {}, type?: SchemaDefinition['type']): PropertyDecorator { - return (target: Prototype, name: string) => { + return (target: unknown, name: string) => { const pathSchema = getMongooseMeta(target).schema[name] || {} type = type || pathSchema['type'] if (!type && !options.type) { @@ -21,7 +22,7 @@ export function prop(options: SchemaType = {}, } export function array(type?: T, options?: SchemaTypeOptions) { - return (target: Prototype, name: string): void => { + return (target: unknown, name: string): void => { let t if (type?.['prototype']?.[mongooseMeta]) { t = getSchema(type as unknown as IMongooseClass) @@ -40,47 +41,47 @@ export function array(type?: T, options?: SchemaTypeOptions {/* empty */} + return (target: unknown, name: string) => {/* empty */} } export function required(): PropertyDecorator { - return (target: Prototype, name: string) => { + return (target: unknown, name: string) => { getMongooseMeta(target).schema[name] = {...getMongooseMeta(target).schema[name], required: true} } } export function indexed(): PropertyDecorator { - return (target: Prototype, name: string) => { + return (target: unknown, name: string) => { getMongooseMeta(target).schema[name] = {...getMongooseMeta(target).schema[name], index: true} } } export function hidden(): PropertyDecorator { - return (target: Prototype, name: string) => { + return (target: unknown, name: string) => { getMongooseMeta(target).schema[name] = {...getMongooseMeta(target).schema[name], select: false} } } export function unique(): PropertyDecorator { - return (target: Prototype, name: string) => { + return (target: unknown, name: string) => { getMongooseMeta(target).schema[name] = {...getMongooseMeta(target).schema[name], unique: true} } } export function defaults(value: T): PropertyDecorator { - return (target: Prototype, name: string) => { + return (target: unknown, name: string) => { getMongooseMeta(target).schema[name] = {...getMongooseMeta(target).schema[name], default: value} } } -export function type(type: Prototype): PropertyDecorator { +export function type(type: unknown): PropertyDecorator { return (target: unknown, name: string) => { getMongooseMeta(target).schema[name] = {...getMongooseMeta(target).schema[name], type} } } export function enums(values: Array | Record): PropertyDecorator { - return (target: Prototype, name: string) => { + return (target: unknown, name: string) => { if (!Array.isArray(values)) { values = Object.values(values) } @@ -88,7 +89,7 @@ export function enums(values: Array | Record Constructor +type LazyClass = () => Constructor export function ref(nameOrClass: string | LazyClass, idType: unknown) export function ref(nameOrClass: IMongooseClass, idType?: unknown) @@ -119,7 +120,7 @@ export function ref(nameOrClass: string | IMongooseClass | LazyClass, idType?: u getMongooseMeta(target).schema[name] = {...field, ref: getMongooseMeta(nameOrClass.prototype).name} } } else { - return (target: Prototype, name: string) => { + return (target: unknown, name: string) => { const field = getMongooseMeta(target).schema[name] || {} const isArray = Array.isArray(field['type']) if (isArray && !Array.isArray(idType)) { @@ -179,25 +180,25 @@ export function refArray(nameOrClass: string | LazyClass | IMongooseClass, eleme } export function statics(): PropertyDecorator { - return (target: Constructor, name: string) => { + return (target: Constructor, name: string) => { getMongooseMeta(target.prototype).statics[name] = target[name] } } export function query(): PropertyDecorator { - return (target: Constructor, name: string) => { + return (target: Constructor, name: string) => { getMongooseMeta(target.prototype).queries[name] = target[name] } } export function methods(): PropertyDecorator { - return (target: Prototype, name: string) => { + return (target: unknown, name: string) => { getMongooseMeta(target).methods[name] = target[name] as Fn } } export function virtual(): MethodDecorator { - return (target: Prototype, name: string, descriptor: PropertyDescriptor) => { + return (target: unknown, name: string, descriptor: PropertyDescriptor) => { if (descriptor.value) { if (typeof descriptor.value !== 'function') { throw new TypeError('virtual can only used on class method or getter/setter')