Skip to content

Commit

Permalink
narrow type
Browse files Browse the repository at this point in the history
  • Loading branch information
emadum committed Jul 20, 2021
1 parent 454d67e commit d0ba75f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 27 deletions.
28 changes: 25 additions & 3 deletions src/operations/indexes.ts
Expand Up @@ -71,7 +71,29 @@ export type IndexSpecification = OneOrMore<
>;

/** @public */
export interface IndexDescription extends CreateIndexesOptions {
export interface IndexDescription
extends Pick<
CreateIndexesOptions,
| 'background'
| 'unique'
| 'partialFilterExpression'
| 'sparse'
| 'hidden'
| 'expireAfterSeconds'
| 'storageEngine'
| 'collation'
| 'version'
| 'weights'
| 'default_language'
| 'language_override'
| 'textIndexVersion'
| '2dsphereIndexVersion'
| 'bits'
| 'min'
| 'max'
| 'bucketSize'
| 'wildcardProjection'
> {
collation?: CollationOptions;
name?: string;
key: Document;
Expand All @@ -96,7 +118,7 @@ export interface CreateIndexesOptions extends CommandOperationOptions {
/** (MongoDB 4.4. or higher) Specifies how many data-bearing members of a replica set, including the primary, must complete the index builds successfully before the primary marks the indexes as ready. This option accepts the same values for the "w" field in a write concern plus "votingMembers", which indicates all voting data-bearing nodes. */
commitQuorum?: number | string;
/** Specifies the index version number, either 0 or 1. */
version: number;
version?: number;
// text indexes
weights?: Document;
default_language?: string;
Expand All @@ -115,7 +137,7 @@ export interface CreateIndexesOptions extends CommandOperationOptions {
// wildcard indexes
wildcardProjection?: Document;
/** Specifies that the index should exist on the target collection but should not be used by the query planner when executing operations. (MongoDB 4.4 or higher) */
hidden: boolean;
hidden?: boolean;
}

function makeIndexSpec(indexSpec: IndexSpecification, options: any): IndexDescription {
Expand Down
45 changes: 21 additions & 24 deletions test/types/index_options.test-d.ts
@@ -1,28 +1,25 @@
import { expectType, expectNotType } from 'tsd';
import { IndexDescription } from '../../src';
import type { IndexDescription } from '../../src';

// test that all valid index options are allowed in IndexDefinitions
expectType<IndexDescription>({
key: {},
background: true,
expireAfterSeconds: 2400,
name: 'index_1',
sparse: true,
storageEngine: {},
unique: true,
version: 1,
default_language: 'english',
language_override: 'english',
textIndexVersion: 2,
weights: {},
'2dsphereIndexVersion': 2,
bits: 1,
max: 1.1,
min: 9.9,
bucketSize: 100,
partialFilterExpression: {},
collation: { locale: 'en' },
wildcardProjection: {},
hidden: true
});
expectType<IndexDescription>({ key: {}, background: true });
expectType<IndexDescription>({ key: {}, expireAfterSeconds: 2400 });
expectType<IndexDescription>({ key: {}, name: 'index_1' });
expectType<IndexDescription>({ key: {}, sparse: true });
expectType<IndexDescription>({ key: {}, storageEngine: {} });
expectType<IndexDescription>({ key: {}, unique: true });
expectType<IndexDescription>({ key: {}, version: 1 });
expectType<IndexDescription>({ key: {}, default_language: 'english' });
expectType<IndexDescription>({ key: {}, language_override: 'english' });
expectType<IndexDescription>({ key: {}, textIndexVersion: 2 });
expectType<IndexDescription>({ key: {}, weights: {} });
expectType<IndexDescription>({ key: {}, '2dsphereIndexVersion': 2 });
expectType<IndexDescription>({ key: {}, bits: 1 });
expectType<IndexDescription>({ key: {}, max: 1.1 });
expectType<IndexDescription>({ key: {}, min: 9.9 });
expectType<IndexDescription>({ key: {}, bucketSize: 100 });
expectType<IndexDescription>({ key: {}, partialFilterExpression: {} });
expectType<IndexDescription>({ key: {}, collation: { locale: 'en' } });
expectType<IndexDescription>({ key: {}, wildcardProjection: {} });
expectType<IndexDescription>({ key: {}, hidden: true });
expectNotType<IndexDescription>({ key: {}, invalidOption: 2400 });

0 comments on commit d0ba75f

Please sign in to comment.