Skip to content

Tighten up types for Entity API #7560

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
52 changes: 41 additions & 11 deletions src/framework/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@ import { getApplication } from './globals.js';
* @import { SpriteComponent } from './components/sprite/component.js'
*/

/**
* @typedef {{
* anim: AnimComponent;
* animation: AnimationComponent;
* audiolistener: AudioListenerComponent;
* button: ButtonComponent;
* camera: CameraComponent;
* collision: CollisionComponent;
* element: ElementComponent;
* gsplat: GSplatComponent;
* layoutchild: LayoutChildComponent;
* layoutgroup: LayoutGroupComponent;
* light: LightComponent;
* model: ModelComponent;
* particlesystem: ParticleSystemComponent;
* render: RenderComponent;
* rigidbody: RigidBodyComponent;
* screen: ScreenComponent;
* script: ScriptComponent;
* scrollbar: ScrollbarComponent;
* scrollview: ScrollViewComponent;
* sound: SoundComponent;
* sprite: SpriteComponent;
* }} ComponentMap A map of component names to their class types.
*/

/**
* @param {Component} a - First object with `order` property.
* @param {Component} b - Second object with `order` property.
Expand Down Expand Up @@ -339,7 +365,8 @@ class Entity extends GraphNode {
* Create a new component and add it to the entity. Use this to add functionality to the entity
* like rendering a model, playing sounds and so on.
*
* @param {string} type - The name of the component to add. Valid strings are:
* @template {keyof ComponentMap} T
* @param {T} type - The name of the component to add. Valid strings are:
*
* - "anim" - see {@link AnimComponent}
* - "animation" - see {@link AnimationComponent}
Expand All @@ -363,10 +390,11 @@ class Entity extends GraphNode {
* - "sound" - see {@link SoundComponent}
* - "sprite" - see {@link SpriteComponent}
*
* @param {object} [data] - The initialization data for the specific component type. Refer to
* each specific component's API reference page for details on valid values for this parameter.
* @returns {Component|null} The new Component that was attached to the entity or null if there
* was an error.
* @param {Partial<ComponentMap[T]>} [data] - The initialization data for the specific
* component type. Refer to each specific component's API reference page for details on valid
* values for this parameter.
* @returns {ComponentMap[T] | null} The new Component that was attached to the entity or null
* if there was an error.
* @example
* const entity = new pc.Entity();
*
Expand Down Expand Up @@ -395,7 +423,7 @@ class Entity extends GraphNode {
/**
* Remove a component from the Entity.
*
* @param {string} type - The name of the Component type.
* @param {keyof ComponentMap} type - The name of the Component type.
* @example
* const entity = new pc.Entity();
* entity.addComponent("light"); // add new light component
Expand All @@ -418,9 +446,10 @@ class Entity extends GraphNode {
/**
* Search the entity and all of its descendants for the first component of specified type.
*
* @param {string} type - The name of the component type to retrieve.
* @returns {Component} A component of specified type, if the entity or any of its descendants
* has one. Returns undefined otherwise.
* @template {keyof ComponentMap} T
* @param {T} type - The name of the component type to retrieve.
* @returns {ComponentMap[T] | undefined} A component of specified type, if the entity or any
* of its descendants has one. Returns undefined otherwise.
* @example
* // Get the first found light component in the hierarchy tree that starts with this entity
* const light = entity.findComponent("light");
Expand All @@ -433,8 +462,9 @@ class Entity extends GraphNode {
/**
* Search the entity and all of its descendants for all components of specified type.
*
* @param {string} type - The name of the component type to retrieve.
* @returns {Component[]} All components of specified type in the entity or any of its
* @template {keyof ComponentMap} T
* @param {T} type - The name of the component type to retrieve.
* @returns {ComponentMap[T][]} All components of specified type in the entity or any of its
* descendants. Returns empty array if none found.
* @example
* // Get all light components in the hierarchy tree that starts with this entity
Expand Down