Skip to main content

Custom Types

TypeScript has multiple ways to support complex data types. However, the Dagger TypeScript SDK only supports it through fields (decorated with @field()) inside classes because type and interface keywords do not allows decorators.

Here is an example of a function named daggerOrganization that returns a custom Organization type, itself containing a collection of Account types:

import { dag, object, func, GitRepository, field } from "@dagger.io/dagger"

@object()
class Account {
@field()
username: string

@field()
email: string

@field()
url: string

constructor(username: string, email: string) {
this.username = username
this.email = email
this.url = `https://github.com/${username}`
}
}

@object()
class Organization {
@field()
url: string

@field()
repository: GitRepository[]

@field()
members: Account[]
}

@object()
class Github {
@func()
daggerOrganization(): Organization {
const organization = new Organization()

organization.url = "https://github.com/dagger"
organization.repository = [dag.git(`${organization.url}/dagger`)]
organization.members = [
new Account("jane", "jane@example.com"),
new Account("john", "john@example.com"),
]

return organization
}
}
note

When the Dagger Engine extends the Dagger API schema with these types, it prefixes their names with the name of the main object:

  • Github
  • GithubAccount
  • GithubOrganization

This is to prevent possible naming conflicts when loading multiple modules, which is reflected in code generation (for example, when using this module in another one as a dependency).

Here's an example of calling this module to get all member URLs:

dagger call dagger-organization members url

The result will be:

https://github.com/jane
https://github.com/john