Skip to content

Update index.d.ts #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 26 additions & 50 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ type Query<T extends unknown[]> = {
/**
* this: Query<T> is necessary to use a colon instead of a period for emits.
*/

/**
* Resets the Iterator for a query.
*/
drain: (this: Query<T>) => Query<T>
/**
* Modifies the query to include specified components, but will not include the values.
* @param components The components to include
* @returns Modified Query
*/
with: (this: Query<T>, ...components: Entity[]) => Query<T>
/**
* Modifies the Query to exclude specified components
Expand All @@ -15,7 +24,7 @@ type Query<T extends unknown[]> = {
* Modifies component data with a callback function
* @param fn The function to modify data
*/
replace: (this: Query<T>, fn: (...components: T) => T extends [infer U] ? U : LuaTuple<T>) => void;
replace: (this: Query<T>, fn: (...components: T) => FlattenTuple<T>) => void;
} & IterableFunction<LuaTuple<[Entity, ...T]>>;

// Utility Types
Expand All @@ -27,6 +36,15 @@ export type InferComponents<A extends Entity[]> = {
type Nullable<T extends unknown[]> = {
[K in keyof T]: T[K] | undefined;
};
type FlattenTuple<T extends any[]> = T extends [infer U] ? U : LuaTuple<T>;

// Utility type for world:get
type TupleForWorldGet =
| [Entity]
| [Entity, Entity]
| [Entity, Entity, Entity]
| [Entity, Entity, Entity, Entity]
| [Entity, Entity, Entity, Entity, Entity]

export class World {
/**
Expand Down Expand Up @@ -64,6 +82,7 @@ export class World {

/**
* Deletes an entity and all its related components and relationships.
* For most situations, you should be using the clear method instead of deletion.
* @param id Entity to be destroyed
*/
delete(id: Entity): void;
Expand Down Expand Up @@ -93,57 +112,14 @@ export class World {
// Manually typed out get since there is a hard limit.

/**
* Retrieves the value of one component. This value may be undefined.
* Retrieves the values of specified components for an entity.
* Some values may not exist when called.
* A maximum of 5 components are allowed at a time.
* @param id Target Entity
* @param component Target Component
* @returns Data associated with the component if it exists
* @param components Target Components
* @returns Data associated with target components if it exists.
*/
get<A>(id: number, component: Entity<A>): A | undefined;

/**
* Retrieves the value of two components. This value may be undefined.
* @param id Target Entity
* @param component Target Component 1
* @param component2 Target Component 2
* @returns Data associated with the components if it exists
*/
get<A, B>(
id: number,
component: Entity<A>,
component2: Entity<B>
): LuaTuple<Nullable<[A, B]>>;

/**
* Retrieves the value of three components. This value may be undefined.
* @param id Target Entity
* @param component Target Component 1
* @param component2 Target Component 2
* @param component3 Target Component 3
* @returns Data associated with the components if it exists
*/
get<A, B, C>(
id: number,
component: Entity<A>,
component2: Entity<B>,
component3: Entity<C>
): LuaTuple<Nullable<[A, B, C]>>;

/**
* Retrieves the value of four components. This value may be undefined.
* @param id Target Entity
* @param component Target Component 1
* @param component2 Target Component 2
* @param component3 Target Component 3
* @param component4 Target Component 4
* @returns Data associated with the components if it exists
*/
get<A, B, C, D>(
id: number,
component: Entity<A>,
component2: Entity<B>,
component3: Entity<C>,
component4: Entity<D>
): LuaTuple<Nullable<[A, B, C, D]>>;
get<T extends TupleForWorldGet>(id: Entity, ...components: T): FlattenTuple<Nullable<InferComponents<T>>>

/**
* Searches the world for entities that match a given query
Expand Down
Loading