diff --git a/.eslintrc.json b/.eslintrc.json index 6540faf937f..a5dc938ab7e 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -147,4 +147,4 @@ "error" ] } -} \ No newline at end of file +} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8570efc5149..c61a517ba9d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -57,3 +57,48 @@ jobs: - name: Linter run: npm run lint + + test-ts-types: + runs-on: ubuntu-latest + name: Typescript Types + steps: + - uses: actions/checkout@v2 + + - name: Setup node + uses: actions/setup-node@v1 + with: + node-version: 12 + + - run: npm install + + - name: Typings + run: npm run test-ts-types + + test-ts-integration: + runs-on: ubuntu-latest + strategy: + fail-fast: false + name: Typescript Integration + steps: + - uses: actions/checkout@v2 + + - name: Setup node + uses: actions/setup-node@v1 + with: + node-version: 12 + + - run: npm install + + - name: Setup + run: | + wget -q https://downloads.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2004-5.0.2.tgz + tar xf mongodb-linux-x86_64-ubuntu2004-5.0.2.tgz + mkdir -p ./data/db/27017 ./data/db/27000 + printf "\n--timeout 8000" >> ./test/mocha.opts + ./mongodb-linux-x86_64-ubuntu2004-5.0.2/bin/mongod --fork --dbpath ./data/db/27017 --syslog --port 27017 + sleep 2 + mongod --version + echo `pwd`/mongodb-linux-x86_64-ubuntu2004-5.0.2/bin >> $GITHUB_PATH + + - run: npm run sym-link-mongoose + - run: npm run test-ts-integration diff --git a/lib/index.js b/lib/index.js index 1fa2e07b5bd..d41b44bc82d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -951,6 +951,8 @@ Mongoose.prototype.ObjectId = SchemaTypes.ObjectId; * mongoose.isValidObjectId({ test: 42 }); // false * * @method isValidObjectId + * @param {Any} value + * @returns {boolean} true if it is a valid ObjectId * @api public */ diff --git a/package.json b/package.json index 25e2ae34c4e..c2f843a15a5 100644 --- a/package.json +++ b/package.json @@ -79,7 +79,7 @@ "test-coverage": "nyc --reporter=html --reporter=text npm test" }, "main": "./index.js", - "types": "./index.d.ts", + "types": "./types/index.d.ts", "engines": { "node": ">=12.0.0" }, @@ -100,6 +100,12 @@ "test/**/*.js" ] }, +<<<<<<< HEAD +======= + "tsd": { + "directory": "test/types" + }, +>>>>>>> a27bfe4a96ffa8f95b52cd1f391c4d3e0105900c "config": { "mongodbMemoryServer": { "disablePostinstall": true diff --git a/test/types/Types.ObjectId.test.ts b/test/types/Types.ObjectId.test.ts new file mode 100644 index 00000000000..14ce7054812 --- /dev/null +++ b/test/types/Types.ObjectId.test.ts @@ -0,0 +1,8 @@ +import * as mongoose from 'mongoose'; +import { expectError, expectType } from 'tsd'; + +const testObjectId = new mongoose.Types.ObjectId(); + +expectType(mongoose.isValidObjectId(testObjectId)); +expectType(mongoose.isValidObjectId('12345')); +expectError(mongoose.isValidObjectId()); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 00000000000..9b5df6e1c5f --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "allowSyntheticDefaultImports": true, + "paths": { + "mongoose" : ["./types/index.d.ts"] + } + }, + "esModuleInterop": true, + "strictNullChecks": true +} diff --git a/index.d.ts b/types/index.d.ts similarity index 99% rename from index.d.ts rename to types/index.d.ts index 26750b7dd21..9b13af9a269 100644 --- a/index.d.ts +++ b/types/index.d.ts @@ -121,6 +121,7 @@ declare module 'mongoose' { * Returns true if Mongoose can cast the given value to an ObjectId, or * false otherwise. */ + export function isValidObjectId(v: Types.ObjectId): true; export function isValidObjectId(v: any): boolean; export function model(name: string, schema?: Schema | Schema, collection?: string, options?: CompileModelOptions): Model;