Skip to content

Commit

Permalink
fix(typings): queryInterface.addIndex (#11844)
Browse files Browse the repository at this point in the history
  • Loading branch information
papb authored and sushantdhiman committed Jan 19, 2020
1 parent fbd19d9 commit 365d7c3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion types/lib/query-interface.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Promise } from './promise';
import QueryTypes = require('./query-types');
import { Sequelize, RetryOptions } from './sequelize';
import { Transaction } from './transaction';
import { SetRequired } from './../type-helpers/set-required';

type BindOrReplacements = { [key: string]: unknown } | unknown[];
type FieldMap = { [key: string]: string };
Expand Down Expand Up @@ -403,7 +404,7 @@ export class QueryInterface {
): Promise<void>;
public addIndex(
tableName: string,
options: QueryInterfaceIndexOptions & { fields: string[] },
options: SetRequired<QueryInterfaceIndexOptions, 'fields'>,
rawTablename?: string
): Promise<void>;

Expand Down
9 changes: 9 additions & 0 deletions types/test/query-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ queryInterface.addIndex('Person', ['firstname', 'lastname'], {
type: 'UNIQUE',
});

queryInterface.addIndex('Foo', {
name: 'foo_a',
fields: [
{ name: 'foo_b', order: 'DESC' },
'foo_c',
{ name: 'foo_d', order: 'ASC', collate: 'foobar', length: 42 }
],
});

queryInterface.removeIndex('Person', 'SuperDuperIndex');

// or
Expand Down
16 changes: 16 additions & 0 deletions types/type-helpers/set-required.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Full credits to sindresorhus/type-fest
*
* https://github.com/sindresorhus/type-fest/blob/v0.8.1/source/set-required.d.ts
*
* Thank you!
*/
export type SetRequired<BaseType, Keys extends keyof BaseType = keyof BaseType> =
// Pick just the keys that are not required from the base type.
Pick<BaseType, Exclude<keyof BaseType, Keys>> &
// Pick the keys that should be required from the base type and make them required.
Required<Pick<BaseType, Keys>> extends
// If `InferredType` extends the previous, then for each key, use the inferred type key.
infer InferredType
? {[KeyType in keyof InferredType]: InferredType[KeyType]}
: never;

0 comments on commit 365d7c3

Please sign in to comment.