PHP package for Laravel to type Eloquent models, routes, Spatie Settings with autogenerated TypeScript.
If you want to use some helpers with Inertia, you can install associated NPM package.
[!NOTE]
kiwilan/typescriptable-laravel
: current PHP package for Laravel.@kiwilan/typescriptable-laravel
: optional NPM package to use with Vite and Inertia to have some helpers, if you want to know more about, check documentation.ziggy
is NOT REQUIRED
- 💽 All Laravel databases are supported: MySQL, PostgreSQL, SQLite, SQL Server
- 💬 Generate TS types for Eloquent models
- 👭 Generate TS types for Eloquent relations
- 🪄 Generate TS types for
casts
(include nativeenum
support) - 📅 Generate TS types for
dates
- 📝 Generate TS types for
appends
withaccessors
- Partial for
Illuminate\Database\Eloquent\Casts\Attribute
- Old way
get*Attribute
methods are totally supported
- Partial for
- #️⃣ Generate TS types for
counts
- 📖 Can generate pagination TS types for Laravel pagination
- 💾 Can generate simple PHP classes from Eloquent models
- ⚙️ Generate TS types for
spatie/laravel-settings
- 🛣 Generate TS types for Laravel routes
- Scan route parameters
- For Inertia, you can install
@kiwilan/typescriptable-laravel
NPM package to use some helpers
- ✅ Multiple commands to generate types
-
php artisan typescriptable
for models, settings and routes (safe even if you don't use all) -
php artisan typescriptable:models
for Eloquent models -
php artisan typescriptable:settings
forspatie/laravel-settings
-
php artisan typescriptable:routes
for Laravel routes
-
- [ ] Improve
Casts\Attribute
methods - [ ] Add parser for calebporzio/sushi
- [ ] Add parser for spatie/laravel-permission
- [ ] Add parser for mongodb
This version requires PHP 8.2+
and supports Laravel 11.
[!WARNING]
Laravel 11 dropped Doctrine DBAL. For previous Laravel versions, you can use
1.12.03
version.
Version | L9 | L10 | L11 |
---|---|---|---|
1.12.03 | ✅ | ✅ | ❌ |
2.0.0 | ❌ | ❌ | ✅ |
You can install the package via composer:
With Laravel v11+ and PHP 8.2
composer require kiwilan/typescriptable-laravel
With Laravel v9-10 and PHP 8.1
composer require kiwilan/typescriptable-laravel:1.12.03
If you want to use .d.ts
files, you need to use TypeScript in your Laravel project, you have to create a tsconfig.json
file and add .d.ts
paths in compilerOptions.types
:
[!NOTE]
If you change paths into config or with options, adapt paths.
{
"compilerOptions": {
"typeRoots": ["./node_modules/@types", "resources/**/*.d.ts"]
},
"include": ["resources/**/*.d.ts"]
}
You can publish the config file
php artisan vendor:publish --tag="typescriptable-config"
php artisan typescriptable
With options:
- --
M
|models
: Generate Models types. - --
R
|routes
: Generate Routes types. - --
S
|settings
: Generate Settings types.
Generate resources/js/types-models.d.ts
file with all models types.
php artisan typescriptable:models
With options:
- --
M
|models-path
: Path to models directory. - --
O
|output-path
: Path to output. - --
P
|php-path
: Path to output PHP classes, if null will not print PHP classes.
If you use spatie/laravel-settings
, you can generate resources/js/types-settings.d.ts
file with all settings types.
php artisan typescriptable:settings
With options:
- --
S
|settings-path
: Path to settings directory. - --
O
|output-path
: Path to output.
Generate resources/js/types-routes.d.ts
file with all routes types and resources/js/routes.ts
for routes references.
php artisan typescriptable:routes
With options:
- --
R
|routes-path
: Path to routes directory. - --
O
|output-path
: Path to output.
If you have a database prefix, you can add it in config/typescriptable.php
file with DB_PREFIX
env variable.
return [
'database_prefix' => env('DB_PREFIX', ''),
];
Or you can use DB_PREFIX
into config/database.php
file.
'prefix' => env('DB_PREFIX', ''),
Two configs works.
kiwilan/typescriptable-laravel
will cover many cases, but if you want to override some models, you can just create a type like resources/js/types/index.ts
and extends Model
type.
interface BookAdvanced extends App.Models.Book {
pivot: {
created_at: string;
updated_at: string;
};
}
And you can import custom type in your code when you need to use advanced type.
Check examples documentation.
Create a .env
file with your database configuration
cp .env.example .env
And you can run tests
composer test
[!NOTE]
To install this on M1 Mac, you need to enable
Use Rosetta for x86/amd64 emulation on Apple Silicon
in Docker preferences.
To install MySQL with Docker
docker run --name mysql \
-e MYSQL_ROOT_PASSWORD=root \
-e MYSQL_USER=testing \
-e MYSQL_PASSWORD=testing \
-e MYSQL_DATABASE=testing \
-p 3306:3306 \
-d \
mysql:8.0
To install PostgreSQL with Docker
docker run --name postgresql \
-e POSTGRES_USER=testing \
-e POSTGRES_PASSWORD=testing \
-e POSTGRES_DB=testing \
-p 5432:5432 \
-d \
postgres:15.4
To install SQL Server with Docker
[!WARNING]
If you have an error like this: "An invalid attribute was designated on the PDO object", you have to update
msphpsql
driver. Check https://github.com/laravel/framework/issues/47937 for more information.
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=12345OHdf%e" \
-p 1433:1433 \
--name sqlserver \
--hostname sqlserver \
-d \
mcr.microsoft.com/mssql/server:2022-latest
Please see CHANGELOG for more information on what has changed recently.
-
Spatie: for
spatie/package-skeleton-laravel
- Ewilan Riviere: Author package
The MIT License (MIT). Please see License File for more information.