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':
// 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:
'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:
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:
// 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.
| Interface | File |
|---|---|
IUser | user.ts |
IUserType | user-type.ts |
IOrderLineData | order-line-data.ts |
JsonLdRawNode | json-ld-raw-node.ts |
Imports
Every file imports exactly the interfaces it references ā nothing more. Cross-references between interfaces become relative type imports:
// 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 ā
IRoleandIMembershipabove. - External
@/ā¦types (fromcustom_props) are re-emitted in each file that uses them, so a type referenced only byIUseris not imported intoIPost. - JSON-LD ā when
with_json_ldis enabled, the sharedJsonLdRawNodeinterface is written tojson-ld-raw-node.tsand imported by the interfaces that use it.
Importing in your app
Import from the directory; the barrel resolves to index.ts:
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.