Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Type Laravel Package

sebastian/type

A collection of value objects that model the PHP type system, including simple, union, intersection, and special types. Use it to inspect, compare, and represent type information programmatically in PHP projects.

View on GitHub
Deep Wiki
Context7

sebastian/type provides a collection of immutable value objects that model the PHP type system, making it possible to inspect, compare, and reason about types programmatically. It powers type handling in PHPUnit and is useful whenever you need a reliable representation of parameter, return, and property types.

  • Value objects for native types, classes, interfaces, and special types such as mixed, void, never, and null
  • Support for union and intersection types, including DNF types
  • Resolution of types from reflection, including iterable key/value types
  • Type comparison and checking through methods like isType() and isSupertypeOf()
  • Strict, dependency-free implementation with a small footprint
Frequently asked questions about Type
How can I use sebastian/type for Laravel validation logic?
sebastian/type lets you model complex types (unions, intersections, nullable) as objects, which you can then use to validate FormRequest payloads or API inputs. For example, create a UnionType for `int|string` and enforce it in custom validation rules. It integrates seamlessly with Laravel’s validation pipeline for type-safe payloads.
Does sebastian/type work with PHPStan or Psalm in Laravel?
Yes, it’s designed for static analysis tools. Use ReflectionMapper to convert PHPStan/Psalm’s type representations into sebastian/type objects, enabling custom rules. For example, enforce return type consistency in services or validate Eloquent model properties. Check phpstan.php for conflicts after upgrading.
What’s the difference between sebastian/type and php-types for Laravel?
sebastian/type focuses on **static type modeling** (e.g., for analyzers, reflection helpers, or test infrastructure), while php-types is runtime-focused (e.g., validating variables at execution). Use sebastian/type for tooling and php-types for runtime checks. They complement each other in Laravel’s type ecosystem.
How do I handle class aliases (e.g., DateTimeImmutable as DateTime) in sebastian/type?
The v7.0.1 fix improves alias resolution, but custom aliases may still need explicit handling. Use ReflectionMapper::fromType() with aliased classes and verify assignability with Type::isAssignableFrom(). For Laravel, test DateTimeImmutable ↔ DateTime bindings in your container or validation logic.
Can I use sebastian/type for OpenAPI or API contract generation in Laravel?
Absolutely. ReflectionMapper converts PHP types (e.g., method return types) into sebastian/type objects, which you can then serialize into OpenAPI schemas. For example, map an Eloquent model’s properties to OpenAPI types using ReflectionMapper::fromPropertyType(). Works well with packages like darkaonline/l5-swagger.
What’s the PHP version requirement for sebastian/type in Laravel?
v7.0.0+ requires PHP 8.4+, while v6.0+ supports PHP 8.3 (Laravel 10’s minimum). Pin to ^6.0 if stuck on PHP 8.3, but note you’ll miss future fixes. Check your Laravel version’s PHP requirements before upgrading.
How do I test sebastian/type in a Laravel project?
Add it as a dev dependency (`composer require --dev sebastian/type`) and test core functionality like ReflectionMapper::fromType() with class aliases. Use Type::isAssignableFrom() to assert type compatibility in unit tests (e.g., mock objects or service return types). Works alongside PestPHP or PHPUnit.
Will sebastian/type break existing PHPStan/Psalm rules in Laravel?
Unlikely, but audit your phpstan.php or psalm.xml for custom rules post-upgrade. The package normalizes types, which may require adjustments to existing rules. For example, union types (e.g., `int|string`) might need reconfiguration. Test with `--level=8` in CI/CD.
Can I extend sebastian/type for custom domain types (e.g., Money, UserId)?
Yes, sebastian/type is composable. Extend the base Type class or create custom value objects (e.g., MoneyType) by implementing the Type interface. Useful for business logic types in Laravel’s validation or DTO layers. Document your custom types for IDE support.
How does sebastian/type improve Laravel’s dependency injection (DI) type safety?
It provides precise type modeling for DI containers, especially with class aliases (e.g., DateTimeImmutable vs. DateTime). Use ReflectionMapper to validate bindings and enforce type consistency in service providers. For example, ensure a `UserRepository` always returns `User` objects, not subclasses.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jinas/dhivatar
store-doc/laravel-storage-driver
plus-it/laravel-translation-manager
diephp/perhaps
diephp/telegraph
osmaviation/spreadsheet
microsoft/fluentui-system-icons
mmccook/php-json-canonicalization-scheme
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
cgv/localized-resource