Separate undefined components and tags (#266)
Some checks failed
analysis / Run Luau Analyze (push) Has been cancelled
deploy-docs / build (push) Has been cancelled
publish-npm / publish (push) Has been cancelled
unit-testing / Run Luau Tests (push) Has been cancelled
deploy-docs / Deploy (push) Has been cancelled

This commit is contained in:
dai 2025-08-12 21:39:04 +03:00 committed by GitHub
parent ac4441eb84
commit 65a27a798a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

14
jecs.d.ts vendored
View file

@ -7,10 +7,14 @@ export type Entity<TData = unknown> = number & {
readonly __type_TData: TData; readonly __type_TData: TData;
}; };
type TagDiscriminator = {
readonly __nominal_Tag: unique symbol;
};
/** /**
* An entity with no associated data when used as a component * An entity with no associated data when used as a component
*/ */
export type Tag = Entity<undefined>; export type Tag = Entity<TagDiscriminator>;
/** /**
* A pair of entities: * A pair of entities:
@ -26,12 +30,12 @@ export type Pair<P = unknown, O = unknown> = number & {
* An `Id` can be either a single Entity or a Pair of Entities. * An `Id` can be either a single Entity or a Pair of Entities.
* By providing `TData`, you can specifically require an Id that yields that type. * By providing `TData`, you can specifically require an Id that yields that type.
*/ */
export type Id<TData = unknown> = Entity<TData> | Pair<TData, unknown> | Pair<undefined, TData>; export type Id<TData = unknown> = Entity<TData> | Pair<TData, unknown> | Pair<TagDiscriminator, TData>;
export type InferComponent<E> = E extends Entity<infer D> export type InferComponent<E> = E extends Entity<infer D>
? D ? D
: E extends Pair<infer P, infer O> : E extends Pair<infer P, infer O>
? P extends undefined ? P extends TagDiscriminator
? O ? O
: P : P
: never; : never;
@ -119,7 +123,7 @@ export class World {
* @returns An entity (Tag) with no data. * @returns An entity (Tag) with no data.
*/ */
entity(): Tag; entity(): Tag;
entity<T extends Entity>(id: T): InferComponent<T> extends undefined ? Tag : T; entity<T extends Entity>(id: T): T;
/** /**
* Creates a new entity in the first 256 IDs, typically used for static * Creates a new entity in the first 256 IDs, typically used for static
@ -148,7 +152,7 @@ export class World {
* @param entity The target entity. * @param entity The target entity.
* @param component The component (or tag) to add. * @param component The component (or tag) to add.
*/ */
add<C>(entity: Entity, component: undefined extends InferComponent<C> ? C : Id<undefined>): void; add<C>(entity: Entity, component: TagDiscriminator extends InferComponent<C> ? C : Id<TagDiscriminator>): void;
/** /**
* Installs a hook on the given component. * Installs a hook on the given component.