Skip to content
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

refactor(components): enhance component accessor type safety with generics #18282

Merged
merged 1 commit into from
Feb 13, 2025
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
18 changes: 9 additions & 9 deletions cocos/scene-graph/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

const idGenerator = new js.IDGenerator('Node');

function getConstructor<T> (typeOrClassName: string | Constructor<T> | AbstractedConstructor<T>): Constructor<T> | AbstractedConstructor<T> | null | undefined {

Check warning on line 61 in cocos/scene-graph/node.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 160. Maximum allowed is 150
if (!typeOrClassName) {
errorID(3804);
return null;
Expand Down Expand Up @@ -318,7 +318,7 @@
return null;
}

protected static _findComponents<T extends Component> (node: Node, constructor: Constructor<T> | AbstractedConstructor<T>, components: Component[]): void {

Check warning on line 321 in cocos/scene-graph/node.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 159. Maximum allowed is 150
const cls = constructor;
const comps = node._components;
// NOTE: internal rtti property
Expand Down Expand Up @@ -877,7 +877,7 @@
* var test = node.getComponent("Test");
* ```
*/
public getComponent(className: string): Component | null;
public getComponent<T extends Component>(className: string): T | null;

public getComponent<T extends Component> (typeOrClassName: string | Constructor<T> | AbstractedConstructor<T>): T | null {
const constructor = getConstructor(typeOrClassName);
Expand All @@ -899,11 +899,11 @@
* @zh 返回节点上指定类型的所有组件。
* @param className The class name of the target component
*/
public getComponents(className: string): Component[];
public getComponents<T extends Component>(className: string): T[];

public getComponents<T extends Component> (typeOrClassName: string | Constructor<T> | AbstractedConstructor<T>): Component[] {
public getComponents<T extends Component> (typeOrClassName: string | Constructor<T> | AbstractedConstructor<T>): T[] {
const constructor = getConstructor(typeOrClassName);
const components: Component[] = [];
const components: T[] = [];
if (constructor) {
Node._findComponents(this, constructor, components);
}
Expand All @@ -930,7 +930,7 @@
* var Test = node.getComponentInChildren("Test");
* ```
*/
public getComponentInChildren(className: string): Component | null;
public getComponentInChildren<T extends Component>(className: string): T | null;

public getComponentInChildren<T extends Component> (typeOrClassName: string | Constructor<T> | AbstractedConstructor<T>): T | null {
const constructor = getConstructor(typeOrClassName);
Expand Down Expand Up @@ -960,11 +960,11 @@
* var tests = node.getComponentsInChildren("Test");
* ```
*/
public getComponentsInChildren(className: string): Component[];
public getComponentsInChildren<T extends Component>(className: string): T[];

public getComponentsInChildren<T extends Component> (typeOrClassName: string | Constructor<T> | AbstractedConstructor<T>): Component[] {
public getComponentsInChildren<T extends Component> (typeOrClassName: string | Constructor<T> | AbstractedConstructor<T>): T[] {
const constructor = getConstructor(typeOrClassName);
const components: Component[] = [];
const components: T[] = [];
if (constructor) {
Node._findComponents(this, constructor, components);
Node._findChildComponents(this._children, constructor, components);
Expand Down Expand Up @@ -994,7 +994,7 @@
* var test = node.addComponent("Test");
* ```
*/
public addComponent(className: string): Component;
public addComponent<T extends Component>(className: string): T;

public addComponent<T extends Component> (typeOrClassName: string | Constructor<T>): T {
if (EDITOR && (this._objFlags & Destroying)) {
Expand Down
Loading