Skip to content

Commit

Permalink
fix: Repair type inference allowing extra keys (#70)
Browse files Browse the repository at this point in the history
* refactor: create type for `InternalKey`

* fix: fix type inference allowing extra keys

* chore: generate changeset
  • Loading branch information
lukemorales committed Jun 19, 2023
1 parent 9c78d22 commit 1a9f74d
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-lies-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lukemorales/query-key-factory': patch
---

Fix type inference allowing extra keys
4 changes: 2 additions & 2 deletions src/create-mutation-keys.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ describe('createMutationKeys', () => {
_def: ['trying to override the _def key value'],
prop: null,
}),
).toThrow('Keys that start with "_" are reserved for the Query Key Factory');
).toThrow('Keys that start with "_" are reserved for Query Key Factory');

expect(() =>
createMutationKeys('users', {
// @ts-expect-error: "_my_own_key" should not be an allowed key
_my_own_key: ['trying to create with the shape of an internal key'],
prop: null,
}),
).toThrow('Keys that start with "_" are reserved for the Query Key Factory');
).toThrow('Keys that start with "_" are reserved for Query Key Factory');
});

describe('when the schema property is not a function', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/create-mutation-keys.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { MutateFunction } from '@tanstack/query-core';

import type { ExtractInternalKeys } from './internals';
import type { ExtractInternalKeys, InternalKey } from './internals';
import type { KeyTuple, AnyMutableOrReadonlyArray, DefinitionKey } from './types';

export type AnyMutationKey = readonly [string, ...any[]];
Expand Down Expand Up @@ -54,7 +54,7 @@ type MutationDynamicKey = (

export type MutationFactorySchema = Record<string, MutationFactoryProperty | MutationDynamicKey>;

type InvalidSchema<Schema extends MutationFactorySchema> = Omit<Schema, `_${string}`>;
type InvalidSchema<Schema extends MutationFactorySchema> = Omit<Schema, InternalKey>;

export type ValidateFactory<Schema extends MutationFactorySchema> = Schema extends {
[P in ExtractInternalKeys<Schema>]: Schema[P];
Expand Down
10 changes: 5 additions & 5 deletions src/create-query-keys.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ describe('createQueryKeys', () => {
it('throws an error if the schema contains a key that starts with "_"', () => {
expect(() =>
createQueryKeys('users', {
// @ts-expect-error: "_def" should not be an allowed key
// @ts-expect-error: "_myOwnKey" should not be an allowed key
_def: ['trying to override the _def key value'],
prop: null,
}),
).toThrow('Keys that start with "_" are reserved for the Query Key Factory');
).toThrow('Keys that start with "_" are reserved for Query Key Factory');

expect(() =>
createQueryKeys('users', {
// @ts-expect-error: "_my_own_key" should not be an allowed key
_my_own_key: ['trying to create with the shape of an internal key'],
// @ts-expect-error: "_myOwnKey" should not be an allowed key
_myOwnKey: ['trying to create with the shape of an internal key'],
prop: null,
}),
).toThrow('Keys that start with "_" are reserved for the Query Key Factory');
).toThrow('Keys that start with "_" are reserved for Query Key Factory');
});

describe('when the schema property is not a function', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/create-query-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export function createQueryKeys<Key extends string>(queryDef: Key): DefinitionKe
export function createQueryKeys<Key extends string, Schema extends QueryFactorySchema>(
queryDef: Key,
schema: ValidateFactory<Schema>,
): QueryKeyFactoryResult<Key, Schema>;
): QueryKeyFactoryResult<Key, ValidateFactory<Schema>>;
export function createQueryKeys<Key extends string, Schema extends QueryFactorySchema>(
queryDef: Key,
schema?: ValidateFactory<Schema>,
): DefinitionKey<[Key]> | QueryKeyFactoryResult<Key, Schema> {
): DefinitionKey<[Key]> | QueryKeyFactoryResult<Key, ValidateFactory<Schema>> {
const defKey: DefinitionKey<[Key]> = {
_def: [queryDef] as const,
};
Expand Down
8 changes: 5 additions & 3 deletions src/create-query-keys.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { QueryFunction } from '@tanstack/query-core';

import type { ExtractInternalKeys } from './internals';
import type { ExtractInternalKeys, InternalKey } from './internals';
import type { KeyTuple, AnyMutableOrReadonlyArray, DefinitionKey } from './types';

export type AnyQueryKey = readonly [string, ...any[]];
Expand Down Expand Up @@ -54,13 +54,15 @@ type DynamicKey = (

export type QueryFactorySchema = Record<string, FactoryProperty | DynamicKey>;

type InvalidSchema<Schema extends QueryFactorySchema> = Omit<Schema, `_${string}`>;
type InvalidSchema<Schema extends QueryFactorySchema> = Omit<Schema, InternalKey>;

export type ValidateFactory<Schema extends QueryFactorySchema> = Schema extends {
[P in ExtractInternalKeys<Schema>]: Schema[P];
}
? InvalidSchema<Schema>
: Schema;
: {
[P in keyof Schema]: Schema[P];
};

type ExtractNullableKey<Key extends KeyTuple | null | undefined> = Key extends
| [...infer Value]
Expand Down
2 changes: 1 addition & 1 deletion src/internals/assert-schema-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const assertSchemaKeys = (schema: Record<string, unknown>): string[] => {
const hasKeyInShapeOfInternalKey = keys.some((key) => key.startsWith('_'));

if (hasKeyInShapeOfInternalKey) {
throw new Error('Keys that start with "_" are reserved for the Query Key Factory');
throw new Error('Keys that start with "_" are reserved for Query Key Factory');
}

return keys;
Expand Down
7 changes: 6 additions & 1 deletion src/internals/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/**
* @internal
*/
export type ExtractInternalKeys<T extends Record<string, unknown>> = Extract<keyof T, `_${string}`>;
export type InternalKey = `_${string}`;

/**
* @internal
*/
export type ExtractInternalKeys<T extends Record<string, unknown>> = Extract<keyof T, InternalKey>;

/**
* Math types for iterating over `mergeQueryKeys` schemas and
Expand Down

0 comments on commit 1a9f74d

Please sign in to comment.