Hyperlinkv0.8.0-beta.28

Config

Config.withDefaultconsteffect/Config.ts:357
<const A2>(defaultValue: A2): <A>(self: Config<A>) => Config<A2 | A>
<A, const A2>(self: Config<A>, defaultValue: A2): Config<A | A2>

Provides a fallback value when the config fails due to missing data.

When to use

Use when you need to make a config key optional with a sensible default.

Gotchas

Only applies when the error is a SchemaError caused exclusively by missing data (missing keys, undefined values). Validation errors (wrong type, out of range) still propagate.

Example (Defaulting a missing port)

import { Config, ConfigProvider, Effect } from "effect"

const port = Config.number("port").pipe(Config.withDefault(3000))

const provider = ConfigProvider.fromUnknown({})
// Effect.runSync(port.parse(provider)) // 3000
combinatorsoptionorElse
Source effect/Config.ts:35714 lines
export const withDefault: {
  <const A2>(defaultValue: A2): <A>(self: Config<A>) => Config<A2 | A>
  <A, const A2>(self: Config<A>, defaultValue: A2): Config<A | A2>
} = dual(2, <A, const A2>(self: Config<A>, defaultValue: A2): Config<A | A2> => {
  return orElse(self, (err) => {
    if (Schema.isSchemaError(err.cause)) {
      const issue = err.cause.issue
      if (isMissingDataOnly(issue)) {
        return succeed(defaultValue)
      }
    }
    return fail(err.cause)
  })
})
Referenced by 2 symbols