KScript docs
EN

Types and validation

Joined types

A type built on other types with fields has all of their fields, rules and methods, merged left to right. Fields it declares itself win. A value of the joined type fits wherever one of its bases is expected:

<type name="User">
  <field name="name" type="string" />
  <field name="email" type="email" />
</type>
<type name="Auditable">
  <field name="created" type="date" />
  <validate test="$created &lt;= @now()" message="created is in the future" />
</type>
<type name="Admin" with="User, Auditable">
  <field name="level" type="int" default="1" />
</type>

<fn name="welcome" params="user: User" value="'Welcome ' + $user.name" />

<let name="admin" value="Admin({ name: 'Joan', email: 'joan@example.com', created: '2026-01-05' })" />
<echo>{ @welcome($admin) } { User->is($admin) } { $admin.level }</echo>
type User {
  name: string
  email: email
}
type Auditable {
  created: date
  validate: $created <= @now(), 'created is in the future'
}
type Admin with [User, Auditable] {
  level: int = 1
}

fn welcome(user: User) => 'Welcome ' + $user.name

$admin = Admin({ name: 'Joan', email: 'joan@example.com', created: '2026-01-05' })
echo "{ @welcome($admin) } { User->is($admin) } { $admin.level }"

When two bases have a field with the same name but different types, the joined type must declare that field itself. The same goes for a method two bases both have: declare it in the joined type to choose. In the editor, the lightbulb on a joined type offers to override each method it gets from its bases, and writes the stub. User->is($admin) says whether a value is a User, and User->is(Admin) whether a type is built on User. A validate: rule in a type with fields sees the fields as $name, $email and so on.

Edit this page on GitHub