updated typescript types

This commit is contained in:
hautajoki 2024-10-11 18:17:31 -07:00
parent 2e246e1e7a
commit 6f6de5e17e

431
src/index.d.ts vendored
View file

@ -1,218 +1,213 @@
/** /**
* Represents an entity * A unique identifier in the world, entity.
* The generic type T defines the data type when this entity is used as a component * The generic type T defines the data type when this entity is used as a component
*/ */
export type Entity<T = unknown> = number & { __T: T }; export type Entity<T = unknown> = number & { __jecs_value: T };
export type Pair = number; /**
* An entity with no associated data when used as a component
export type Id<T = unknown> = Entity<T> | Pair; */
export type Tag = Entity<undefined>;
type FlattenTuple<T extends any[]> = T extends [infer U] ? U : LuaTuple<T>;
type Nullable<T extends unknown[]> = { [K in keyof T]: T[K] | undefined }; /**
type InferComponents<A extends Id[]> = { [K in keyof A]: A[K] extends Id<infer T> ? T : never }; * A pair of entities
type TupleForWorldGet = [Id] | [Id, Id] | [Id, Id, Id] | [Id, Id, Id, Id]; * P is the type of the predicate, O is the type of the object, and V is the type of the value (defaults to P)
*/
type Item<T extends unknown[]> = (this: Query<T>) => LuaTuple<[Entity, ...T]>; export type Pair<P = undefined, O = undefined, V = P> = number & {
__jecs_pair_pred: P;
export type Query<T extends unknown[]> = { __jecs_pair_obj: O;
/** __jecs_pair_value: V;
* Get the next result in the query. Drain must have been called beforehand or otherwise it will error. };
*/
next: Item<T>; /**
* Either an Entity or a Pair
/** */
* Resets the Iterator for a query. export type Id<T = unknown> = Entity<T> | Pair<unknown, unknown, T>;
*/
drain: (this: Query<T>) => Query<T>; type InferComponent<E> = E extends Id<infer T> ? T : never;
type FlattenTuple<T extends any[]> = T extends [infer U] ? U : LuaTuple<T>;
/** type Nullable<T extends unknown[]> = { [K in keyof T]: T[K] | undefined };
* Modifies the query to include specified components, but will not include the values. type InferComponents<A extends Id[]> = {
* @param components The components to include [K in keyof A]: InferComponent<A[K]>;
* @returns Modified Query };
*/ type TupleForWorldGet = [Id] | [Id, Id] | [Id, Id, Id] | [Id, Id, Id, Id];
with: (this: Query<T>, ...components: Id[]) => Query<T>;
type Iter<T extends unknown[]> = IterableFunction<LuaTuple<[Entity, ...T]>>;
/**
* Modifies the Query to exclude specified components export type Query<T extends unknown[]> = {
* @param components The components to exclude /**
* @returns Modified Query * Returns an iterator that returns a tuple of an entity and queried components
*/ */
without: (this: Query<T>, ...components: Id[]) => Query<T>; iter(): Iter<T>;
/** /**
* Modifies component data with a callback function * Modifies the query to include specified components
* @param fn The function to modify data * @param components The components to include
*/ * @returns Modified Query
replace: (this: Query<T>, fn: (...components: T) => FlattenTuple<T>) => void; */
with(...components: Id[]): Query<T>;
/**
* Returns the archetypes associated with this query. /**
*/ * Modifies the Query to exclude specified components
archetypes: () => Archetype[]; * @param components The components to exclude
} & IterableFunction<LuaTuple<[Entity, ...T]>>; * @returns Modified Query
*/
export type Archetype = { without(...components: Id[]): Query<T>;
id: number; } & Iter<T>;
edges: { [key: number]: ArchetypeEdge };
types: number[]; export class World {
type: string | number; /**
entities: number[]; * Creates a new World
columns: unknown[][]; */
records: { [key: number]: ArchetypeRecord }; constructor();
};
/**
type ArchetypeRecord = { * Creates a new entity
count: number; * @returns Entity
column: number; */
}; entity(): Tag;
type ArchetypeEdge = { /**
add: Archetype; * Creates a new entity located in the first 256 ids.
remove: Archetype; * These should be used for static components for fast access.
}; * @returns Entity<T>
*/
export class World { component<T = unknown>(): Entity<T>;
/**
* Creates a new World /**
*/ * Gets the target of a relationship. For example, when a user calls
constructor(); * `world.target(entity, ChildOf(parent))`, you will obtain the parent entity.
* @param entity Entity
/** * @param relation The Relationship
* Creates a new entity * @returns The Parent Entity if it exists
* @returns Entity */
*/ target(entity: Entity, relation: Entity): Entity | undefined;
entity(): Entity;
/**
/** * Gets the target of a relationship at a specific index.
* Creates a new entity located in the first 256 ids. * For example, when a user calls `world.target(entity, ChildOf(parent), 0)`,
* These should be used for static components for fast access. * you will obtain the parent entity.
* @returns Entity<T> * @param entity Entity
*/ * @param relation The Relationship
component<T = unknown>(): Entity<T>; * @param index Target index
* @returns The Parent Entity if it exists
/** */
* Gets the target of a relationship. For example, when a user calls target(entity: Entity, relation: Entity, index: number): Entity | undefined;
* `world.target(entity, ChildOf(parent))`, you will obtain the parent entity.
* @param entity Entity /**
* @param index Target index * Clears an entity from the world
* @param relation The Relationship * @param entity Entity to be cleared
* @returns The Parent Entity if it exists */
*/ clear(entity: Entity): void;
target(entity: Entity, relation: Entity, index: number): Entity | undefined;
/**
/** * Deletes an entity and all its related components and relationships
* Gets the target of a relationship. For example, when a user calls * @param entity Entity to be destroyed
* `world.target(entity, ChildOf(parent))`, you will obtain the parent entity. */
* @param entity Entity delete(entity: Entity): void;
* @param relation The Relationship
* @returns The Parent Entity if it exists /**
*/ * Adds a component to the entity with no value
target(entity: Entity, relation: Entity): Entity | undefined; * @param entity Target Entity
* @param component Component
/** */
* Clears an entity from the world. add(entity: Entity, component: Id): void;
* @param entity Entity to be cleared
*/ /**
clear(entity: Entity): void; * Assigns a value to a component on the given entity
* @param entity Target Entity
/** * @param component Target Component
* Deletes an entity and all its related components and relationships. * @param value Component Value
* @param entity Entity to be destroyed */
*/ set<E extends Id<unknown>>(entity: Entity, component: E, value: InferComponent<E>): void;
delete(entity: Entity): void;
/**
/** * Removes a component from the given entity
* Adds a component to the entity with no value * @param entity Target Entity
* @param entity Target Entity * @param component Target Component
* @param component Component */
*/ remove(entity: Entity, component: Id): void;
add<T>(entity: Entity, component: Id<T>): void;
/**
/** * Retrieves the values of specified components for an entity.
* Assigns a value to a component on the given entity * Some values may not exist when called.
* @param entity Target Entity * A maximum of 4 components are allowed at a time.
* @param component Target Component * @param id Target Entity
* @param data Component Data * @param components Target Components
*/ * @returns Data associated with target components if it exists.
set<T>(entity: Entity, component: Id<T>, data: NoInfer<T>): void; */
get<T extends TupleForWorldGet>(id: Entity, ...components: T): FlattenTuple<Nullable<InferComponents<T>>>;
/**
* Removes a component from the given entity /**
* @param entity Target Entity * Returns whether the entity has the specified components.
* @param component Target Component * A maximum of 4 components are allowed at a time.
*/ * @param entity Target Entity
remove(entity: Entity, component: Id): void; * @param components Target Components
* @returns If the entity contains the components
/** */
* Retrieves the values of specified components for an entity. has(entity: Entity, ...components: Id[]): boolean;
* Some values may not exist when called.
* A maximum of 4 components are allowed at a time. /**
* @param id Target Entity * Checks if an entity exists in the world
* @param components Target Components * @param entity Entity to check
* @returns Data associated with target components if it exists. * @returns Whether the entity exists in the world
*/ */
get<T extends TupleForWorldGet>(id: Entity, ...components: T): FlattenTuple<Nullable<InferComponents<T>>>; contains(entity: Entity): boolean;
/** /**
* Returns whether the entity has the specified components. * Get parent (target of ChildOf relationship) for entity.
* A maximum of 4 components are allowed at a time. * If there is no ChildOf relationship pair, it will return undefined.
* @param entity Target Entity * @param entity Target Entity
* @param components Target Components * @returns Parent Entity or undefined
* @returns If the entity contains the components */
*/ parent(entity: Entity): Entity | undefined;
has(entity: Entity, ...components: Id[]): boolean;
/**
/** * Searches the world for entities that match a given query
* Checks if an entity exists in the world * @param components Queried Components
* @param entity Entity to check * @returns Query
* @returns Whether the entity exists in the world */
*/ query<T extends Id[]>(...components: T): Query<InferComponents<T>>;
contains(entity: Entity): boolean; }
/** /**
* Searches the world for entities that match a given query * Creates a composite key (pair)
* @param components Queried Components * @param pred The first entity (predicate)
* @returns Query * @param obj The second entity (object)
*/ * @returns The composite key (pair)
query<T extends Id[]>(...components: T): Query<InferComponents<T>>; */
export function pair<P, O, V = P>(pred: Entity<P>, obj: Entity<O>): Pair<P, O, V>;
/**
* Get parent (target of ChildOf relationship) for entity. /**
* If there is no ChildOf relationship pair, it will return undefined. * Checks if the entity is a composite key (pair)
* @param entity Target Entity * @param value The entity to check
* @returns Parent Entity or undefined * @returns If the entity is a pair
*/ */
parent(entity: Entity): Entity | undefined; export function IS_PAIR(value: Id): value is Pair;
}
/**
/** * Gets the first entity (predicate) of a pair
* Creates a composite key. * @param pair The pair to get the first entity from
* @param pred The first entity * @returns The first entity (predicate) of the pair
* @param obj The second entity */
* @returns The composite key export function pair_first<P, O, V = P>(pair: Pair<P, O, V>): Entity<P>;
*/
export function pair(pred: Entity, obj: Entity): Pair; /**
* Gets the second entity (object) of a pair
/** * @param pair The pair to get the second entity from
* Checks if the entity is a composite key * @returns The second entity (object) of the pair
* @param e The entity to check */
* @returns If the entity is a pair export function pair_second<P, O, V = P>(pair: Pair<P, O, V>): Entity<O>;
*/
export function IS_PAIR(e: Id): boolean; export const OnAdd: Entity<(e: Entity) => void>;
export const OnRemove: Entity<(e: Entity) => void>;
/** Built-in Component used to find every component id */ export const OnSet: Entity<(e: Entity, value: unknown) => void>;
export const Component: Entity; export const ChildOf: Entity;
export const Wildcard: Entity;
export const OnAdd: Entity<(e: Entity) => void>; export const w: Entity;
export const OnRemove: Entity<(e: Entity) => void>; export const OnDelete: Entity;
export const OnSet: Entity<(e: Entity, value: unknown) => void>; export const OnDeleteTarget: Entity;
export const ChildOf: Entity; export const Delete: Entity;
export const Wildcard: Entity; export const Remove: Entity;
export const w: Entity; export const Name: Entity<string>;
export const OnDelete: Entity; export const Rest: Entity;
export const OnDeleteTarget: Entity;
export const Delete: Entity;
export const Remove: Entity;
export const Name: Entity<string>;
export const Rest: Entity;