Skip to content

Commit

Permalink
Fix Join type - fix key selection on Get operations
Browse files Browse the repository at this point in the history
  • Loading branch information
mtranter committed Mar 2, 2023
1 parent 36449e7 commit 1e1c628
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
11 changes: 10 additions & 1 deletion src/lib/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ describe('Table', () => {
readonly id: string;
readonly name: string;
};
// eslint-disable-next-line functional/prefer-readonly-type
readonly someArray?: string[];
};
};

Expand Down Expand Up @@ -413,19 +415,26 @@ describe('Table', () => {
id: '123',
name: 'fred',
},
someArray: ['Fred'],
},
};
const setup = async () => {
await compoundTable.put(key);
return compoundTable.get(key, {
keys: ['gsihash', 'sort', 'complexSubDoc.subSubDoc.id'],
keys: [
'gsihash',
'sort',
'complexSubDoc.subSubDoc.id',
'complexSubDoc.someArray',
],
});
};
it('Should get selected keys', async () => {
const result = await setup();
expect(result?.gsihash).toEqual(key.gsihash);
expect(result?.sort).toEqual(key.sort);
expect(result?.complexSubDoc.subSubDoc.id).toEqual('123');
expect(result?.complexSubDoc.someArray).toEqual(['Fred']);
expect((result?.complexSubDoc.subSubDoc as any).name).toBeUndefined();
expect((result?.complexSubDoc as any).id).toBeUndefined();
});
Expand Down
16 changes: 7 additions & 9 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@ type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
export type NestedKeyOf<T, D extends number = 7> = [D] extends [never]
? never
: T extends object
?
| (keyof T & string)
| { [K in keyof T]-?: Join<K, NestedKeyOf<T[K], Prev[D]>> }[keyof T]
? { [K in keyof T]-?: Join<K, NestedKeyOf<T[K], Prev[D]>> }[keyof T]
: '';

type Join<K, P> = K extends string
? P extends string
? `${K}${'' extends P ? '' : '.'}${P}`
: never
: never;

export type UnionToIntersection<U, Default = never> = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
U extends any
Expand All @@ -70,12 +74,6 @@ export type UnionToIntersection<U, Default = never> = (
? I
: Default;

type Join<K, P> = K extends string | number
? P extends string | number
? `${K}${'' extends P ? '' : '.'}${P}`
: never
: never;

type _NestedPick<
A,
K extends string,
Expand Down

0 comments on commit 1e1c628

Please sign in to comment.