Compare commits

..

1 commit

Author SHA1 Message Date
PepeElToro41
c72b6ec682
Merge f912866fcb into ac4441eb84 2025-08-10 18:13:17 +02:00

14
jecs.d.ts vendored
View file

@ -7,14 +7,10 @@ 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<TagDiscriminator>; export type Tag = Entity<undefined>;
/** /**
* A pair of entities: * A pair of entities:
@ -30,12 +26,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<TagDiscriminator, TData>; export type Id<TData = unknown> = Entity<TData> | Pair<TData, unknown> | Pair<undefined, 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 TagDiscriminator ? P extends undefined
? O ? O
: P : P
: never; : never;
@ -123,7 +119,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): T; entity<T extends Entity>(id: T): InferComponent<T> extends undefined ? Tag : 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
@ -152,7 +148,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: TagDiscriminator extends InferComponent<C> ? C : Id<TagDiscriminator>): void; add<C>(entity: Entity, component: undefined extends InferComponent<C> ? C : Id<undefined>): void;
/** /**
* Installs a hook on the given component. * Installs a hook on the given component.