type-lang 2.0Type language for PHPPHP 8.1+
TypeLang is a declarative type language inspired by PHPStan and Psalm. It describes the types you already
write in doc-comments — int[], array<string, T>, Foo::CONST_*,
T is U ? A : B — and ships the parts to work with them: a parser, a printer, a reflection reader
and a PHPDoc parser. The tree below is drawn by a JavaScript port of the reference parser. Type a type.
Pretty printer
§ 01
Every construct in the language composes with every other. Start from a name and keep wrapping.
Any identifier is a type: a built-in, a class, an alias, even a hyphenated non-empty-string. The parser reads it as a NamedTypeNode. It checks that a type is well-formed, not that it exists — resolution is a separate concern.
A leading question mark wraps the type in a NullableTypeNode. It binds tighter than |, so ?A|B is (?A)|B.
Angle brackets carry generics. Any type goes inside, trailing commas are fine, * stands for “whatever”, and covariant, contravariant or invariant may precede an argument.
Unions are flat: A|B|C is one node with three children, not two nested pairs. Intersections use & and bind tighter.
A second argument describes the key. array-key, int, literal strings — the parser doesn't care which; your tool decides what they mean.
Braces turn a name into a shape: a list of fields, each with its own type. Keys can be identifiers, numbers, quoted strings or class constants; a field with no key is implicit.
Shape types →int<1, max> is just a generic with two literal-ish arguments. The trailing ... says “more keys may follow”, and may even describe them: ...<string, mixed>.
A question mark after the key marks the field optional. Notice how little of the tree has to move.
Shape types →Any name followed by parentheses is a callable type. Parameters may be named, variadic, by-reference or carry a default; the return type follows a colon and binds tightly: callable(): A|B is (callable(): A)|B.
A named callable like Closure can declare template parameters: variance, of / as and super bounds, and defaults. The return type here is conditional: T is Paid ? Bill : null, or T is not Paid ? … to negate it.
§ 02
Every construct the grammar knows, set as a specimen. Hover a bracket to see the node behind it.
§ 03
Every grammar construct is a flag on TypeParserFeatures. Switch some off and the parser
rejects the syntax with a message that says why. Native PHP types only? Turn everything off but names.
§ 04
237 syntactic constructs, the basic ones and those on which PHPStan, Psalm and TypeLang disagree, each written as @return and read by the tool's own docblock reader.
A construct counts when the whole text becomes the type it was written as, with nothing left for the description.
Full table with the verdict for every construct, per tool, in the syntax comparison. The table also lists 33 malformed inputs that must be rejected: TypeLang rejects 33, PHPStan 29 and Psalm 15.
§ 05
One grammar and three things built on it. Every package is published standalone and split from a single development monorepo.
01
The syntax of the types, how it reads and how other tools read it.
No package: the grammar every other part reads.
Read the docs →02
Parse a type into an AST, print it back, read it from Reflection.
type-lang/typestype-lang/parsertype-lang/printertype-lang/reader03
Map raw data to objects and back by the types you declare.
type-lang/mappertype-lang/mapper-symfonytype-lang/mapper-laravel04
Read doc-comments into a description and typed tags.
type-lang/phpdoc