Multi-file Output

Generate one file per interface with an index barrel

Multi-file Output

By default the package writes every interface into a single concatenated file at output_path. For larger projects you can split the output into one file per interface by setting output_mode to 'multiple':

php
// config/oi-laravel-ts.php
'output_mode' => 'multiple',
'output_dir' => resource_path('js/types'),

In multiple mode output_path is ignored and files are written to output_dir instead.

Barrel file name

The barrel re-export file defaults to index.ts. If that name conflicts with an existing file in your project, override it with barrel_file:

php
'barrel_file' => 'interfaces.ts',

The generated layout then becomes:

resources/js/types/
ā”œā”€ā”€ interfaces.ts   // re-exports every interface
ā”œā”€ā”€ user.ts
└── post.ts

And you import from the named file instead:

typescript
import type { IUser } from '@/types/interfaces';

Generated layout

resources/js/types/
ā”œā”€ā”€ index.ts        // re-exports every interface
ā”œā”€ā”€ user.ts
ā”œā”€ā”€ post.ts
ā”œā”€ā”€ role.ts
└── membership.ts

Each interface lives in its own file, and index.ts is a barrel that re-exports all of them:

typescript
// index.ts
export * from './membership';
export * from './post';
export * from './role';
export * from './user';

File naming

File names are the kebab-case of the interface name, with the leading I stripped. The interfaces keep their I prefix in code.

InterfaceFile
IUseruser.ts
IUserTypeuser-type.ts
IOrderLineDataorder-line-data.ts
JsonLdRawNodejson-ld-raw-node.ts

Imports

Every file imports exactly the interfaces it references — nothing more. Cross-references between interfaces become relative type imports:

typescript
// user.ts
import type { IMembership } from './membership';
import type { IPost } from './post';
import type { IRole } from './role';

export interface IUser {
    posts?: IPost[];
    roles?: IRole[];
    memberships?: (IRole & { pivot?: IMembership })[];
}

A few details worth noting:

  • Pivot intersections import both sides — IRole and IMembership above.
  • External @/… types (from custom_props) are re-emitted in each file that uses them, so a type referenced only by IUser is not imported into IPost.
  • JSON-LD — when with_json_ld is enabled, the shared JsonLdRawNode interface is written to json-ld-raw-node.ts and imported by the interfaces that use it.

Importing in your app

Import from the directory; the barrel resolves to index.ts:

typescript
import type { IUser, IPost } from '@/types';

const user: IUser = await fetchUser(1);

Switching back to a single file

Set output_mode back to 'single' (the default). The single-file output is unchanged from previous versions — same bytes, same output_path.

Project under MIT License.
Design by