refactor(components): enhance component accessor type safety with generics - #18282
Conversation
|
@cocos-robot run test cases |
|
@9romise, Please check the result of
Task Details
|
|
@9romise, Please check the result of
Task Details |
|
@cocos-robot run test cases |
|
@9romise, Please check the result of
Task Details
|
|
@9romise, Please check the result of
Task Details
|
|
This code will break the compatibilty in engine, for example, In spine/skeleton.ts. debugDraw = debugDrawNode.addComponent('cc.Graphics') as Graphics;ESLint report: This assertion is unnecessary since it does not change the type of the expression. If I save the file, eslint configuration will automatically remove debugDraw = debugDrawNode.addComponent('cc.Graphics');But it causes another TS compiler error: Yes, we could modify the code to debugDraw = debugDrawNode.addComponent<Graphics>('cc.Graphics');to resolve the issue in engine. But if our users also write the same kinds of code, they will complain about the bad compatibilty. And I think the two kinds of debugDraw = debugDrawNode.addComponent('cc.Graphics'); // Old way and new way both return `Component | null` type
debugDraw = debugDrawNode.addComponent<Graphics>('cc.Graphics'); // New way in this PR, Return Graphics type
debugDraw = debugDrawNode.addComponent('cc.Graphics') as Graphics; // Old way, Return Graphics typeI suggest to revert this PR for better compatibility. What do you think? |
|
@dumganhar can we keep both of the two usage? |
|
@minggo It seems we could add the new overrode |
Re: #
Changelog
Before:
After:
Continuous Integration
This pull request:
Compatibility Check
This pull request: