kiwilan/typescriptable-laravel
Install the PHP package:
composer require kiwilan/typescriptable-laravel
Publish the config (optional but recommended for customization):
php artisan vendor:publish --provider="Kiwilan\Typescriptable\TypescriptableServiceProvider"
Run the generator for Eloquent models:
php artisan typescriptable:generate
resources/js/types/eloquent/.First use case: Use the generated types in your Inertia/Vue/React components:
import type { User } from '@/types/eloquent/User';
const user: User = await getUser(); // Type-safe access to model attributes
config/typescriptable.php – Customize output paths, naming conventions, and included models.resources/js/types/ – Default output directory for generated TypeScript files.app/Models/ – Ensure your Eloquent models are properly defined (e.g., casts, relations).Automatic Generation:
Run php artisan typescriptable:generate to generate types for all Eloquent models in app/Models/.
php artisan typescriptable:generate --models=User,Post --output=custom/path
--models to generate types for specific models only.--output to override the default output directory.Including/Excluding Models:
Configure config/typescriptable.php to specify which models to include/exclude:
'models' => [
'include' => ['App\Models\User', 'App\Models\Post'],
'exclude' => ['App\Models\TempModel'],
],
Handling Relations:
The package automatically infers relation types (e.g., belongsTo, hasMany). Example:
// Generated for a User model with a 'posts' relation
interface User {
posts: Post[];
}
Generate Route Types:
php artisan typescriptable:routes
Outputs types for Laravel routes to resources/js/types/routes.ts.
Example:
interface Routes {
'users.index': '/users';
'users.show': (id: number) => `/users/${id}`;
}
Integration with Inertia: Use the generated route types in Inertia links:
import { routes } from '@/types/routes';
<Link href={routes['users.show'](user.id)}>View User</Link>
php artisan typescriptable:settings
Outputs types for Spatie Laravel Settings to resources/js/types/settings.ts.
Example:
interface AppSettings {
site_name: string;
maintenance_mode: boolean;
}
Override Default Naming:
Use the type_map config to customize how models are named in TypeScript:
'type_map' => [
'App\Models\User' => 'AppUser', // Custom type name
],
Excluding Attributes:
Use the ignored_attributes config to exclude sensitive fields:
'ignored_attributes' => ['password', 'api_token'],
npm install @kiwilan/typescriptable-laravel
useTypescriptable composable in Vue/Inertia:
<script setup>
import { useTypescriptable } from '@kiwilan/typescriptable-laravel';
const { getModelType } = useTypescriptable();
const userType = getModelType('App\Models\User'); // Returns 'User'
</script>
Circular Dependencies in Relations:
User has Post, Post has User), the generator may fail or produce incomplete types.--force flag or manually adjust the generated types:
php artisan typescriptable:generate --force
Dynamic Attributes or JSON Columns:
json: array).protected $casts = [
'metadata' => 'array<string, mixed>',
];
Namespace Conflicts:
class, interface), the generator may fail.type_map config to rename the types:
'type_map' => [
'App\Models\ClassName' => 'ClassNameModel',
],
Inertia Page Typing:
import { PageProps } from '@inertiajs/core';
import type { User } from '@/types/eloquent/User';
interface UserShowPageProps extends PageProps {
user: User;
}
Check Generator Logs:
Run the generator with --verbose to debug issues:
php artisan typescriptable:generate --verbose
Validate Model Definitions: Ensure all Eloquent models:
App\Models\).belongsTo, hasMany).$append, $hidden) that break type inference.Clear Cache: If types aren’t updating, clear Laravel and TypeScript caches:
php artisan cache:clear
npm run dev
Partial Generation: Regenerate only specific models for faster iterations:
php artisan typescriptable:generate --models=User
Watch Mode (NPM): Use the NPM package to watch for model changes and auto-regenerate types:
npm run typescriptable:watch
Custom TypeScript Config:
Extend the generated types in a separate file (e.g., resources/js/types/extensions.ts):
declare module '@/types/eloquent/User' {
interface User {
fullName: string; // Computed property
}
}
MongoDB Support:
For MongoDB models, ensure the jenssegers/laravel-mongodb package is installed and models extend \Jenssegers\Mongodb\Eloquent\Model.
CI/CD Integration: Add the generator to your CI pipeline to ensure types are always up-to-date:
# Example GitHub Actions step
- run: php artisan typescriptable:generate
How can I help you explore Laravel packages today?