Commit ae79e6c 1 parent 5669ab9 commit ae79e6c Copy full SHA for ae79e6c
File tree 2 files changed +15
-4
lines changed
2 files changed +15
-4
lines changed Original file line number Diff line number Diff line change @@ -203,11 +203,15 @@ export class Entity extends EngineObject {
203
203
/**
204
204
* Add component based on the component type.
205
205
* @param type - The type of the component
206
+ * @param args - The arguments of the component
206
207
* @returns The component which has been added
207
208
*/
208
- addComponent < T extends Component > ( type : new ( entity : Entity ) => T ) : T {
209
+ addComponent < T extends new ( entity : Entity , ...args : any [ ] ) => Component > (
210
+ type : T ,
211
+ ...args : ComponentArguments < T >
212
+ ) : InstanceType < T > {
209
213
ComponentsDependencies . _addCheck ( this , type ) ;
210
- const component = new type ( this ) ;
214
+ const component = new type ( this , ... args ) as InstanceType < T > ;
211
215
this . _components . push ( component ) ;
212
216
component . _setActive ( true , ActiveChangeFlag . All ) ;
213
217
return component ;
@@ -751,3 +755,10 @@ export class Entity extends EngineObject {
751
755
return this . _invModelMatrix ;
752
756
}
753
757
}
758
+
759
+ type ComponentArguments < T extends new ( entity : Entity , ...args : any [ ] ) => Component > = T extends new (
760
+ entity : Entity ,
761
+ ...args : infer P
762
+ ) => Component
763
+ ? P
764
+ : never ;
Original file line number Diff line number Diff line change @@ -60,7 +60,7 @@ export class XRManagerExtended extends XRManager {
60
60
override addFeature < T extends new ( xrManager : XRManagerExtended , ...args : any [ ] ) => XRFeature > (
61
61
type : T ,
62
62
...args : TFeatureConstructorArguments < T >
63
- ) : XRFeature | null {
63
+ ) : InstanceType < T > | null {
64
64
if ( this . sessionManager . _platformSession ) {
65
65
throw new Error ( "Cannot add feature when the session is initialized." ) ;
66
66
}
@@ -69,7 +69,7 @@ export class XRManagerExtended extends XRManager {
69
69
const feature = features [ i ] ;
70
70
if ( feature instanceof type ) throw new Error ( "The feature has been added" ) ;
71
71
}
72
- const feature = new type ( this , ...args ) ;
72
+ const feature = new type ( this , ...args ) as InstanceType < T > ;
73
73
this . _features . push ( feature ) ;
74
74
return feature ;
75
75
}
You can’t perform that action at this time.
0 commit comments