Skip to content

Commit 3d2c66b

Browse files
committed
feat(NODE-3331): offer downleveled types for legacy typescript versions
1 parent 1a57ba8 commit 3d2c66b

File tree

7 files changed

+76
-6
lines changed

7 files changed

+76
-6
lines changed

.evergreen/run-checks.sh

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
#!/bin/bash
2-
set -o errexit # Exit the script with error if any of the commands fail
2+
set -o errexit # Exit the script with error if any of the commands fail
33

44
export PROJECT_DIRECTORY="$(pwd)"
55
NODE_ARTIFACTS_PATH="${PROJECT_DIRECTORY}/node-artifacts"
66
export NVM_DIR="${NODE_ARTIFACTS_PATH}/nvm"
77
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
88

9+
set -o xtrace
10+
911
npm run check:lint
12+
13+
echo "Typescript $(npx tsc -v)"
14+
# check resolution uses the default latest types
15+
echo "import * as mdb from '.'" > file.ts && npx tsc --noEmit --traceResolution file.ts | grep 'mongodb.d.ts' && rm file.ts
16+
17+
npm i --no-save [email protected] # there is no 4.0.0
18+
echo "Typescript $(npx tsc -v)"
19+
npx tsc --noEmit mongodb.ts34.d.ts
20+
# check that resolution uses the downleveled types
21+
echo "import * as mdb from '.'" > file.ts && npx tsc --noEmit --traceResolution file.ts | grep 'mongodb.ts34.d.ts' && rm file.ts

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ For version compatibility matrices, please refer to the following links:
4141
- [MongoDB](https://docs.mongodb.com/drivers/node/compatibility#mongodb-compatibility)
4242
- [NodeJS](https://docs.mongodb.com/drivers/node/compatibility#language-compatibility)
4343

44+
#### Typescript Version
45+
46+
We recommend using the latest version of typescript, however we do provide a [downleveled](https://github.com/sandersn/downlevel-dts#readme) version of the type definitions that we test compiling against `[email protected]`.
47+
Since typescript [does not restrict breaking changes to major versions](https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes) we consider this support best effort.
48+
If you run into any unexpected compiler failures please let us know and we will do our best to correct it.
49+
4450
## Installation
4551

4652
The recommended way to get started using the Node.js 4.0 driver is by using the `npm` (Node Package Manager) to install the dependency in your project.

package-lock.json

+37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+11-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@
77
"lib",
88
"src",
99
"etc/prepare.js",
10-
"mongodb.d.ts"
10+
"mongodb.d.ts",
11+
"mongodb.ts34.d.ts"
1112
],
1213
"types": "mongodb.d.ts",
14+
"typesVersions": {
15+
"<=4.0.2": {
16+
"mongodb.d.ts": [
17+
"mongodb.ts34.d.ts"
18+
]
19+
}
20+
},
1321
"repository": {
1422
"type": "git",
1523
"url": "[email protected]:mongodb/node-mongodb-native.git"
@@ -48,6 +56,7 @@
4856
"chalk": "^4.1.0",
4957
"co": "4.6.0",
5058
"coveralls": "^3.0.11",
59+
"downlevel-dts": "^0.7.0",
5160
"eslint": "^7.26.0",
5261
"eslint-config-prettier": "^6.11.0",
5362
"eslint-plugin-jsdoc": "^30.7.8",
@@ -94,7 +103,7 @@
94103
"scripts": {
95104
"build:evergreen": "node .evergreen/generate_evergreen_tasks.js",
96105
"build:ts": "rimraf lib && tsc",
97-
"build:dts": "npm run build:ts && api-extractor run && rimraf 'lib/**/*.d.ts*'",
106+
"build:dts": "npm run build:ts && api-extractor run && rimraf 'lib/**/*.d.ts*' && downlevel-dts mongodb.d.ts mongodb.ts34.d.ts",
98107
"build:docs": "typedoc",
99108
"check:bench": "node test/benchmarks/driverBench",
100109
"check:coverage": "nyc npm run check:test",

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ export type {
376376
FilterOperators,
377377
BSONTypeAlias,
378378
BitwiseFilter,
379-
RegExpOrString
379+
RegExpOrString,
380+
OneOrMore
380381
} from './mongo_types';
381382
export type { serialize, deserialize } from './bson';

src/mongo_types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ export type SchemaMember<T, V> = { [P in keyof T]?: V } | { [key: string]: V };
186186
/** @public */
187187
export type Nullable<AnyType> = AnyType | null | undefined;
188188

189+
/** @public */
190+
export type OneOrMore<T> = T | T[];
191+
189192
/** @public */
190193
export type GenericListener = (...args: any[]) => void;
191194

src/operations/indexes.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type { Db } from '../db';
2222
import { AbstractCursor } from '../cursor/abstract_cursor';
2323
import type { ClientSession } from '../sessions';
2424
import { executeOperation, ExecutionResult } from './execute_operation';
25+
import type { OneOrMore } from '../mongo_types';
2526

2627
const LIST_INDEXES_WIRE_VERSION = 3;
2728
const VALID_INDEX_OPTIONS = new Set([
@@ -58,14 +59,15 @@ const VALID_INDEX_OPTIONS = new Set([
5859

5960
/** @public */
6061
export type IndexDirection = -1 | 1 | '2d' | '2dsphere' | 'text' | 'geoHaystack' | number;
62+
6163
/** @public */
62-
export type IndexSpecification =
64+
export type IndexSpecification = OneOrMore<
6365
| string
6466
| [string, IndexDirection]
6567
| { [key: string]: IndexDirection }
6668
| [string, IndexDirection][]
6769
| { [key: string]: IndexDirection }[]
68-
| IndexSpecification[];
70+
>;
6971

7072
/** @public */
7173
export interface IndexDescription {

0 commit comments

Comments
 (0)