Skip to content

Commit db1043d

Browse files
committed
🏷️ core: disallow object literals and arrays on schemas, simplify trait types
1 parent 170f25c commit db1043d

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

packages/core/src/trait/trait.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ import { createStore } from './utils/create-store';
1717

1818
let traitId = 0;
1919

20-
function defineTrait(): Trait<Schema>;
21-
function defineTrait<S extends Schema>(schema: S): Trait<Norm<S>>;
22-
function defineTrait<S extends Schema>(schema: S = {} as S): Trait<any> {
20+
function defineTrait<S extends Schema>(schema: S = {} as S): Trait<Norm<S>> {
2321
const isAoS = typeof schema === 'function';
2422
const traitType: TraitType = isAoS ? 'aos' : 'soa';
2523

packages/core/src/trait/types.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ export type TraitInstance<T extends Trait | Schema> = T extends Trait
6969

7070
export type Schema =
7171
| {
72-
[key: string]: number | bigint | string | boolean | any[] | object | null | undefined;
72+
[key: string]: number | bigint | string | boolean | null | undefined | (() => unknown);
7373
}
7474
| AoSFactory;
7575

76-
export type AoSFactory = () => Record<string, any>;
76+
export type AoSFactory = () => unknown;
7777

7878
export type Store<T extends Schema = any> = T extends AoSFactory
7979
? ReturnType<T>[]
@@ -83,8 +83,8 @@ export type Store<T extends Schema = any> = T extends AoSFactory
8383

8484
// Type Utils
8585

86-
// This type utility ensures that explicit values like true, false or "string literal" are normalized to their primitive types.
87-
// Mostly used for schema types.
86+
// This type utility ensures that explicit values like true, false or "string literal" are
87+
// normalized to their primitive types. Mostly used for schema types.
8888
export type Norm<T extends Schema> = T extends AoSFactory
8989
? () => ReturnType<T> extends number
9090
? number
@@ -93,6 +93,16 @@ export type Norm<T extends Schema> = T extends AoSFactory
9393
: ReturnType<T> extends string
9494
? string
9595
: ReturnType<T>
96+
: {
97+
[K in keyof T]: T[K] extends object
98+
? T[K] extends (...args: any[]) => any
99+
? T[K]
100+
: never
101+
: T[K] extends boolean
102+
? boolean
103+
: T[K];
104+
}[keyof T] extends never
105+
? never
96106
: {
97107
[K in keyof T]: T[K] extends boolean ? boolean : T[K];
98108
};

packages/core/tests/query-modifiers.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212

1313
const Position = trait({ x: 0, y: 0 });
1414
const IsActive = trait();
15-
const Foo = trait({});
16-
const Bar = trait({});
15+
const Foo = trait();
16+
const Bar = trait();
1717

1818
describe('Query modifiers', () => {
1919
const world = createWorld();

packages/core/tests/query.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { $internal, cacheQuery, createWorld, IsExcluded, Not, Or, trait } from '
44
const Position = trait({ x: 0, y: 0 });
55
const Name = trait({ name: 'name' });
66
const IsActive = trait();
7-
const Foo = trait({});
8-
const Bar = trait({});
7+
const Foo = trait();
8+
const Bar = trait();
99

1010
describe('Query', () => {
1111
const world = createWorld();

0 commit comments

Comments
 (0)