@@ -349,21 +349,28 @@ private function const(array $genericNodes): ConstantT
349349
350350 private function callable (CallableTypeNode $ node ): CallableT
351351 {
352- $ templates = [];
353-
354- foreach ($ node ->templateTypes as $ templateNode ) {
355- $ template = new Template ($ templateNode ->name );
356- $ templates [$ templateNode ->name ] = $ template ;
357- $ this ->templateTypes [$ templateNode ->name ] = $ template ->type ;
358- }
359-
360352 return new CallableT (
361353 templates: array_map (
362- fn (TemplateTagValueNode $ node ): Template => $ templates [$ node ->name ]
363- ->withLowerBound ($ node ->lowerBound === null ? neverT : $ this ->parse ($ node ->lowerBound ))
364- ->withUpperBound ($ node ->bound === null ? mixedT : $ this ->parse ($ node ->bound ))
365- ->withDefault ($ node ->default === null ? null : $ this ->parse ($ node ->default )),
366- $ node ->templateTypes ,
354+ // 4. resolve templates
355+ static fn (\Closure $ lazyTemplate ): Template => $ lazyTemplate (),
356+ array_map (
357+ function (TemplateTagValueNode $ node ): \Closure {
358+ // 2. create template factory
359+ $ factory = Template::factory (
360+ name: $ node ->name ,
361+ // 1. assign template type by reference
362+ type: $ this ->templateTypes [$ node ->name ],
363+ );
364+
365+ // 3. apply the rest of the template's properties to the factory
366+ return fn (): Template => $ factory (
367+ lowerBound: $ node ->lowerBound === null ? neverT : $ this ->parse ($ node ->lowerBound ),
368+ upperBound: $ node ->bound === null ? mixedT : $ this ->parse ($ node ->bound ),
369+ default: $ node ->default === null ? null : $ this ->parse ($ node ->default ),
370+ );
371+ },
372+ $ node ->templateTypes ,
373+ ),
367374 ),
368375 parameters: array_map (
369376 fn (CallableTypeParameterNode $ node ): Parameter => new Parameter (
0 commit comments