nolanos / laravel-model-typescript-transfomer
Generate Typescript definitions for your Eloquent models
Fund package maintenance!
ncphillips
Requires
- php: ^8.2
- laravel/framework: ^9.0|^10.0|^11.0
- spatie/laravel-typescript-transformer: ^2.4
Requires (Dev)
- mockery/mockery: ^1.6
- orchestra/testbench: ^9.4
- pestphp/pest: ^2.35
README
This package generates TypeScript definitions for Laravel Eloquent models using spatie/laravel-typescript-transformer.
Installation
Install via Composer:
composer require nolanos/laravel-model-typescript-transformer
Update your config/typescript-transformer.php
config file by...
- Appending
ModelTypeScriptCollector
to thecollectors
array. - Appending
ModelTransformer
to thetransformers
array.
'collectors' => [ // ... Nolanos\LaravelModelTypescriptTransformer\ModelTypeScriptCollector::class, ], 'transformers' => [ // Etc.... Nolanos\LaravelModelTypescriptTransformer\ModelTransformer::class, ],
Usage
Generate TypeScript definitions by running:
php artisan typescript:generate
How it works
Identifying Properties
The ModelTransformer
identifies the properties of the given Model
by looking at the columns in the database table, and then
filters columns out based on the $hidden
and $visible
properties of the model.
Mapping Database Types to TypeScript Types
Property types are determined by mapping the database column type to a TypeScript type. The following mappings are used:
Note: Unknown column types will be mapped to unknown
and are followed by a comment stating the db type.
Nullable Types
If a column is nullable, the TypeScript type will be suffixed with | null
.
Example
Let's look at a simple of example of a user model in a PostgreSQL database.
CREATE TABLE users ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL , password VARCHAR(255), active BOOLEAN NOT NULL, created_at TIMESTAMP, updated_at TIMESTAMP );
The User
model might look like this:
class User extends Model { protected $hidden = ['password']; }
Will generate the following TypeScript definition:
declare namespace App.Models { export interface User { id: number name: string email: string active: boolean created_at: string | null updated_at: string | null } }
Limitations
This package has some limitations.
Take a look at the issues to see what's missing.
Development
Setup
git clone git@github.com:nolanos/laravel-model-typescript-transfomer.git
cd laravel-model-typescript-transformer
composer install
Running Tests
composer test
Publishing new Versions
To publish a new version of the package, you need to create a new tag and push it to the repository.
git tag vx.x.x git push origin vx.x.x
Go to Packagist and click on "Update" to update the package.