A serializer DSL that is also the source of a TypeScript interface.
defmodule MyAppWeb.TaskSerializer do
use Typelizer.Serializer, schema: MyApp.Tasks.Task
attributes [:id, :title, :status, :due_on, :inserted_at]
attribute :overdue, type: :boolean, value: &MyApp.Tasks.overdue?/1
has_one :assignee, serializer: MyAppWeb.UserSerializer, nullable: true
has_many :comments, serializer: MyAppWeb.CommentSerializer
end
MyAppWeb.TaskSerializer.serialize(task)
#=> %{"id" => "…", "title" => "…", "dueOn" => "2026-10-01", …}
MyAppWeb.TaskSerializer.serialize_many(tasks, user_id: current_user.id)mix typelizer.gen writes the matching TypeScript interface.
Options
:schema- an Ecto schema or embedded schema. Types are inferred from it and each attribute is checked against it.:name- the TypeScript interface name. Default: derived from the module name (MyAppWeb.Admin.TaskSerializer→AdminTask).:key_transform-:camelor:snake. Default:config :typelizer, :key_transform(:camel).
Generated functions
serialize(record, opts \\ [])- returns a map with string keys, ornilfornil.serialize_many(records, opts \\ [])- returns a list of maps.
The options are passed to arity-2 value: functions and to nested serializers.
See the Serializers guide for type specs, type inference and nullability.
Summary
Functions
Declares one field.
Declares a computed field. The function is the last argument, so the options must be written in brackets
Declares schema fields. Needs the :schema option.
Declares a list of nested objects, serialized with another serializer.
Declares one nested object, serialized with another serializer.
Functions
Declares one field.
Options
:type- a type spec. Required with:valueor without a schema.:nullable-trueorfalse. Always wins over inference.:value- a function of the record (arity 1) or of the record and the options (arity 2) that computes the value.:optional-trueleaves the key out when the value is nil (key?: T).:required- for an embed: the embed fields that are never null ([:street, geo: [:lat]]), or:all.:if- a function (arity 1 or 2) that decides whether the key is sent (key?: T).
Examples
attribute :title
attribute :title, nullable: false
attribute :overdue, type: :boolean, value: &MyApp.Tasks.overdue?/1
attribute :overdue, [type: :boolean], fn task -> MyApp.Tasks.overdue?(task) end
Declares a computed field. The function is the last argument, so the options must be written in brackets:
attribute :overdue, [type: :boolean], fn task -> MyApp.Tasks.overdue?(task) end
Declares schema fields. Needs the :schema option.
attributes [:id, :title, :status]
Declares a list of nested objects, serialized with another serializer.
Options
:serializer- required. The serializer of each nested value.:value- a function (arity 1 or 2) that returns the nested values.:optional,:if- as forattribute/2.
Examples
has_many :comments, serializer: MyAppWeb.CommentSerializer
Declares one nested object, serialized with another serializer.
Options
:serializer- required. The serializer of the nested value.:nullable-trueorfalse.:value- a function (arity 1 or 2) that returns the nested value.:optional,:if- as forattribute/2.
Examples
has_one :assignee, serializer: MyAppWeb.UserSerializer, nullable: true