Skip to content

Commit 338532f

Browse files
committed
[union] Preserve MergeableStore compatibility
1 parent c2d3885 commit 338532f

5 files changed

Lines changed: 38 additions & 21 deletions

File tree

‎src/@types/mergeable-store/with-schemas/index.d.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,17 @@ export interface MergeableStore<
244244
setTablesSchema<const TS extends TablesSchema>(
245245
tablesSchema: TS & ValidTablesSchema<TS>,
246246
): MergeableStore<[TS, Schemas[1]]>;
247+
setTablesSchema<const TS extends TablesSchema>(
248+
tablesSchema: TS & ValidTablesSchema<TS>,
249+
): Store<[TS, Schemas[1]]>;
247250

248251
/// Store.setValuesSchema
249252
setValuesSchema<const VS extends ValuesSchema>(
250253
valuesSchema: VS & ValidValuesSchema<VS>,
251254
): MergeableStore<[Schemas[0], VS]>;
255+
setValuesSchema<const VS extends ValuesSchema>(
256+
valuesSchema: VS & ValidValuesSchema<VS>,
257+
): Store<[Schemas[0], VS]>;
252258

253259
/// Store.setSchema
254260
setSchema<const TS extends TablesSchema, const VS extends ValuesSchema>(
@@ -262,6 +268,17 @@ export interface MergeableStore<
262268
: VS,
263269
]
264270
>;
271+
setSchema<const TS extends TablesSchema, const VS extends ValuesSchema>(
272+
tablesSchema: TS & ValidTablesSchema<TS>,
273+
valuesSchema?: VS & ValidValuesSchema<VS>,
274+
): Store<
275+
[
276+
TS,
277+
Exclude<ValuesSchema, typeof valuesSchema> extends never
278+
? NoValuesSchema
279+
: VS,
280+
]
281+
>;
265282

266283
/// Store.delTablesSchema
267284
delTablesSchema<

‎test/unit/core/store/store-nulls.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {Store} from 'tinybase';
1+
import type {Store, TablesSchema} from 'tinybase';
22
import {createStore} from 'tinybase';
33
import {beforeEach, describe, expect, test} from 'vitest';
44

@@ -161,7 +161,7 @@ describe('Null with schemas', () => {
161161
t1: {
162162
c1: {type: 'string', default: null},
163163
},
164-
});
164+
} as TablesSchema);
165165

166166
expect(result).toEqual(store);
167167
expect(store.getTablesSchemaJson()).toEqual('{}');

‎test/unit/core/store/store-schema.test.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
Tables,
99
TablesSchema,
1010
Value,
11+
ValuesSchema,
1112
} from 'tinybase/with-schemas';
1213
import {createMergeableStore, createStore} from 'tinybase/with-schemas';
1314
import {
@@ -250,7 +251,7 @@ describe.each([
250251
test('Validate enum Cell schemas', () => {
251252
store.setTablesSchema({
252253
pets: {status: {enum: ['draft', 'live'], default: 'other'}},
253-
});
254+
} as TablesSchema);
254255
expect(JSON.parse(store.getTablesSchemaJson())).toEqual({
255256
pets: {status: {enum: ['draft', 'live']}},
256257
});
@@ -567,7 +568,7 @@ describe.each([
567568
test('Validate enum Value schemas', () => {
568569
store.setValuesSchema({
569570
status: {enum: ['draft', 'live'], default: 'other'},
570-
});
571+
} as ValuesSchema);
571572
expect(JSON.parse(store.getValuesSchemaJson())).toEqual({
572573
status: {enum: ['draft', 'live']},
573574
});
@@ -3282,15 +3283,15 @@ describe.each([
32823283
c4: {type: 'array', default: null, allowNull: true},
32833284
},
32843285
};
3285-
const valuesSchema = {
3286-
v1: {type: 'object' as const, default: {a: 1}},
3287-
v2: {type: 'array' as const, default: [2, 3]},
3286+
const valuesSchema: ValuesSchema = {
3287+
v1: {type: 'object', default: {a: 1}},
3288+
v2: {type: 'array', default: [2, 3]},
32883289
v3: {
3289-
type: 'object' as const,
3290+
type: 'object',
32903291
default: null,
32913292
allowNull: true,
32923293
},
3293-
v4: {type: 'array' as const, default: null, allowNull: true},
3294+
v4: {type: 'array', default: null, allowNull: true},
32943295
};
32953296
const expectedTablesSchema = structuredClone(tablesSchema);
32963297
const expectedValuesSchema = structuredClone(valuesSchema);

‎test/unit/core/types/__snapshots__/types.test.tsx.snap‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,27 +2358,27 @@ exports[`Types in store.ts > 1961:36 1`] = `"Argument of type 'false' is not ass
23582358

23592359
exports[`Types in store.ts > 1962:33 1`] = `"Argument of type '{}' is not assignable to parameter of type 'Value<{ readonly name: { readonly type: "string"; readonly allowNull: true; }; readonly profile: { readonly type: "object"; readonly default: null; readonly allowNull: true; }; readonly tags: { readonly type: "array"; readonly default: null; readonly allowNull: true; }; }, "tags", "array", never, null> | MapValue<...>'."`;
23602360

2361-
exports[`Types in store-schema-defaults.ts > 10:6 1`] = `"Type '{ type: string[]; default: number; }' is not assignable to type 'never'."`;
2361+
exports[`Types in store-schema-defaults.ts > 9:6 1`] = `"Type '{ type: string[]; default: number; }' is not assignable to type 'never'."`;
23622362

2363-
exports[`Types in store-schema-defaults.ts > 14:4 1`] = `"Type '{ enum: string[]; default: string; }' is not assignable to type 'never'."`;
2363+
exports[`Types in store-schema-defaults.ts > 13:4 1`] = `"Type '{ enum: string[]; default: string; }' is not assignable to type 'never'."`;
23642364

2365-
exports[`Types in store-schema-defaults.ts > 17:12 1`] = `"Type '{ type: string; default: null; }' is not assignable to type 'never'."`;
2365+
exports[`Types in store-schema-defaults.ts > 16:12 1`] = `"Type '{ type: string; default: null; }' is not assignable to type 'never'."`;
23662366

2367-
exports[`Types in store-schema-defaults.ts > 25:6 1`] = `"Type '{ enum: string[]; default: string; }' is not assignable to type 'never'."`;
2367+
exports[`Types in store-schema-defaults.ts > 24:6 1`] = `"Type '{ enum: string[]; default: string; }' is not assignable to type 'never'."`;
23682368

2369-
exports[`Types in store-schema-defaults.ts > 29:4 1`] = `"Type '{ type: string[]; default: number; }' is not assignable to type 'never'."`;
2369+
exports[`Types in store-schema-defaults.ts > 28:4 1`] = `"Type '{ type: string[]; default: number; }' is not assignable to type 'never'."`;
23702370

2371-
exports[`Types in store-schema-defaults.ts > 32:12 1`] = `"Type '{ type: string; default: null; }' is not assignable to type 'never'."`;
2371+
exports[`Types in store-schema-defaults.ts > 31:12 1`] = `"Type '{ type: string; default: null; }' is not assignable to type 'never'."`;
23722372

2373-
exports[`Types in store-schema-defaults.ts > 44:6 1`] = `"Type '{ type: string[]; default: number; }' is not assignable to type 'never'."`;
2373+
exports[`Types in store-schema-defaults.ts > 43:6 1`] = `"No overload matches this call."`;
23742374

2375-
exports[`Types in store-schema-defaults.ts > 48:4 1`] = `"Type '{ enum: string[]; default: string; }' is not assignable to type 'never'."`;
2375+
exports[`Types in store-schema-defaults.ts > 47:4 1`] = `"No overload matches this call."`;
23762376

2377-
exports[`Types in store-schema-defaults.ts > 52:6 1`] = `"Type '{ type: string; default: null; }' is not assignable to type 'never'."`;
2377+
exports[`Types in store-schema-defaults.ts > 51:6 1`] = `"No overload matches this call."`;
23782378

2379-
exports[`Types in store-schema-defaults.ts > 79:42 1`] = `"Type 'DefaultedCellFromSchema<{ pets: { answer: { type: readonly ["string", "boolean"]; default: 1; }; }; }, "pets", "answer">' does not satisfy the expected type 'string | boolean'."`;
2379+
exports[`Types in store-schema-defaults.ts > 78:42 1`] = `"Type 'DefaultedCellFromSchema<{ pets: { answer: { type: readonly ["string", "boolean"]; default: 1; }; }; }, "pets", "answer">' does not satisfy the expected type 'string | boolean'."`;
23802380

2381-
exports[`Types in store-schema-defaults.ts > 81:27 1`] = `"Type 'DefaultedValueFromSchema<{ status: { enum: readonly ["draft", "live"]; default: "archived"; }; }, "status">' does not satisfy the expected type '"draft" | "live"'."`;
2381+
exports[`Types in store-schema-defaults.ts > 80:27 1`] = `"Type 'DefaultedValueFromSchema<{ status: { enum: readonly ["draft", "live"]; default: "archived"; }; }, "status">' does not satisfy the expected type '"draft" | "live"'."`;
23822382

23832383
exports[`Types in ui-react.tsx > 137:31 1`] = `"Property 't2' does not exist on type 'Tables<{ readonly t0: { readonly c0: { readonly type: "number"; }; }; readonly t1: { readonly c1: { readonly type: "number"; }; readonly c1d: { readonly type: "string"; readonly default: ""; }; }; }, false>'."`;
23842384

‎test/unit/core/types/store-schema-defaults.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Store schema default types
21
// NB: an exclamation mark after a line visually indicates an expected TS error
32
import {createStore as createStoreWithoutSchemas} from 'tinybase';
43
import {createMergeableStore} from 'tinybase/mergeable-store/with-schemas';

0 commit comments

Comments
 (0)