@@ -274,16 +274,16 @@ private function getTypeScriptType(stdClass $columnSchema): string {
274274 // We generate new interfaces for any relational attributes.
275275 // That means we can recursively instantiate the current class to generate
276276 // as many interface definitions for relational attributes as we need.
277- return (new self ($ fullyQualifiedRelatedModelName ))->generate ();
277+ $ mappedType = (new self ($ fullyQualifiedRelatedModelName ))->generate ();
278+ } else {
279+ // If the attribute is natively casted, we'll want to perform native cast checking
280+ // to generate the correct TypeScript type. If it's not natively casted, we can
281+ // simply map the database type to a TypeScript type.
282+ $ mappedType = $ this ->isAttributeNativelyCasted ($ columnSchema ->Field )
283+ ? $ this ->mapNativeCastToTypeScriptType ($ columnSchema ->Field )
284+ : $ this ->mapDatabaseTypeToTypeScriptType ($ columnType );
278285 }
279286
280- // If the attribute is natively casted, we'll want to perform native cast checking
281- // to generate the correct TypeScript type. If it's not natively casted, we can
282- // simply map the database type to a TypeScript type.
283- $ mappedType = $ this ->isAttributeNativelyCasted ($ columnSchema ->Field )
284- ? $ this ->mapNativeCastToTypeScriptType ($ columnSchema ->Field )
285- : $ this ->mapDatabaseTypeToTypeScriptType ($ columnType );
286-
287287 // We can't do much with an unknown type.
288288 if ($ mappedType === 'unknown ' ) return $ mappedType ;
289289
@@ -313,19 +313,22 @@ private function generateInterface(): string {
313313
314314 $ outputBuffer = collect ([
315315 // The output buffer always needs to start with the first `interface X {` line.
316- 'interface ' . ( Str::of ($ this ->fullyQualifiedModelName )->afterLast ('\\' )) . " { "
316+ sprintf ( 'interface %s { ' , Str::of ($ this ->fullyQualifiedModelName )->afterLast ('\\' )),
317317 ]);
318318
319319 $ tableColumns ->each (function ($ column ) use ($ outputBuffer ) {
320320 // If this attribute is hidden and we're not including hidden, we'll skip it.
321321 if (!$ this ->includeHidden && $ this ->isAttributeHidden ($ column ->Field )) return ;
322322
323323 if ($ this ->isAttributeRelation ($ column ->Field )) {
324- // The foreign model name will be the name of the interface we'll generate for this relation.
325- $ foreignModelName = Str::of ($ this ->convertForeignKeyToFullyQualifiedModelName ($ column ->Field ))->afterLast ('\\' );
326324 $ relationName = $ this ->convertForeignKeyToPredictedRelationName ($ column ->Field );
325+ $ generatedTypeScriptType = Str::of ($ this ->getTypeScriptType ($ column ));
326+
327+ $ generatedTypeName = $ generatedTypeScriptType
328+ ->after ('interface ' )
329+ ->before (' { ' ) . $ generatedTypeScriptType ->afterLast ('} ' );
327330
328- $ outputBuffer ->push (sprintf (' %s: %s; ' , $ relationName , $ foreignModelName ));
331+ $ outputBuffer ->push (sprintf (' %s: %s; ' , $ relationName , $ generatedTypeName ));
329332
330333 // Add an empty line so related interfaces aren't directly after each other.
331334 $ outputBuffer ->prepend ('' );
@@ -335,7 +338,7 @@ private function generateInterface(): string {
335338 // Once we've got the interface, we'll want to explode it, then prepend
336339 // each piece of the interface to the output buffer.
337340 // We reverse the exploded string because we prepend, otherwise we'd prepend backwards.
338- Str:: of ( $ this -> getTypeScriptType ( $ column ))
341+ $ generatedTypeScriptType
339342 ->explode ("\n" )
340343 ->reverse ()
341344 ->each (fn ($ str ) => $ outputBuffer ->prepend ($ str ));
0 commit comments