Skip to content

Constants and Literals

What Constants Are

Constants define reusable literal values.

const apiVersion = "1.0.0"
const maxRetries = 3
const featureEnabled = true

Constants do not have explicit type annotations. VDL infers their value kind from the literal.

Correct:

const timeoutMs = 2500

Invalid:

const timeoutMs int = 2500

Scalar Literals

VDL supports these scalar literal forms:

const textValue = "hello"
const intValue = 42
const floatValue = 3.14
const trueValue = true
const falseValue = false

Strings use double quotes.

Object Literals

Object literals use braces and space-separated key/value entries.

const serverConfig = {
  host "localhost"
  port 8080
  tls false
}

There are no colons and no commas.

Object keys must be unique inside the same object.

Nested Object Literals

const appConfig = {
  server {
    host "localhost"
    port 8080
  }
  logging {
    level "info"
  }
}

Array Literals

Array items are separated by whitespace.

const roles = ["admin" "editor" "viewer"]
const retryBackoffMs = [100 250 500]

All items in an array literal must have the same value kind.

Invalid:

const mixedValues = ["admin" 1 true]

Constant References

Constants can reference other constants.

const defaultTimeoutMs = 5000
const requestTimeoutMs = defaultTimeoutMs

References use the constant name directly.

Enum Member References

Constants can reference enum members with EnumName.MemberName.

enum ProductStatus {
  Draft = "draft"
  Published = "published"
}

const defaultStatus = ProductStatus.Draft

The referenced enum and member must exist.

Object Spreads

Object literals can spread another constant object.

const baseConfig = {
  host "localhost"
  port 8080
}

const productionConfig = {
  ...baseConfig
  host "api.example.com"
}

Only constants whose value is an object literal can be used in object spreads.

Spreads use the Name form. Name.Member is not valid for object spreads.

Constants In Annotations

Because annotation arguments are data literals, constants can also help share annotation values.

const owner = "platform"

@meta({ team owner })
type ServiceConfig {
  name string
}

Whether a plugin understands a particular annotation is plugin-specific.

Naming

Constant names should use camelCase.

const apiVersion = "1.0.0"
const maxPageSize = 100