Skip to content

Commit a93ef91

Browse files
pavloconuvestephenplusplus
authored andcommitted
fix: assign more restrictive type to PathType (#433) (#435)
This prevents `Key path element must not be incomplete` runtime error, and also allows using big int (datastore.Int) when creating a key using KeyOptions object as an argument.
1 parent 7ee76b3 commit a93ef91

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

src/entity.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import arrify = require('arrify');
1818
import * as extend from 'extend';
1919
import * as is from 'is';
2020
import {Query, QueryProto} from './query';
21+
import {PathType} from '.';
2122

2223
// tslint:disable-next-line no-namespace
2324
export namespace entity {
@@ -162,7 +163,7 @@ export namespace entity {
162163

163164
export interface KeyOptions {
164165
namespace?: string;
165-
path: Array<string | number>;
166+
path: PathType[];
166167
}
167168

168169
/**

src/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ import {Transaction} from './transaction';
3737

3838
const {grpc} = new GrpcClient();
3939

40-
// tslint:disable-next-line: no-any
41-
export type PathType = any;
40+
export type PathType = string | number | entity.Int;
4241

4342
// Import the clients for each version supported by this package.
4443
const gapic = Object.freeze({
@@ -701,13 +700,13 @@ class Datastore extends DatastoreRequest {
701700
* });
702701
*/
703702
key(options: string | entity.KeyOptions | PathType[]): entity.Key {
704-
options = is.object(options)
705-
? options
703+
const keyOptions = is.object(options)
704+
? (options as entity.KeyOptions)
706705
: {
707706
namespace: this.namespace,
708707
path: arrify(options) as PathType[],
709708
};
710-
return new entity.Key(options as entity.KeyOptions);
709+
return new entity.Key(keyOptions);
711710
}
712711

713712
/**

system-test/datastore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ describe('Datastore', () => {
159159
const [entity] = await datastore.get(postKey);
160160
delete entity[datastore.KEY];
161161
assert.deepStrictEqual(entity, data);
162-
await datastore.delete(datastore.key(['Post', assignedId]));
162+
await datastore.delete(datastore.key(['Post', assignedId as string]));
163163
});
164164

165165
it('should save/get/delete with a generated key id', async () => {

0 commit comments

Comments
 (0)