<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)) // 3000export const const withDefault: {
<A2>(defaultValue: A2): <A>(
self: Config<A>
) => Config<A2 | A>
<A, 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
withDefault: {
<const function (type parameter) A2 in <const A2>(defaultValue: A2): <A>(self: Config<A>) => Config<A2 | A>A2>(defaultValue: const A2defaultValue: function (type parameter) A2 in <const A2>(defaultValue: A2): <A>(self: Config<A>) => Config<A2 | A>A2): <function (type parameter) A in <A>(self: Config<A>): Config<A2 | A>A>(self: Config<A>(parameter) self: {
parse: (provider: ConfigProvider.ConfigProvider, pathPrefix?: Path) => Effect.Effect<T, ConfigError>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self: interface Config<out T>A recipe for extracting a typed value T from a ConfigProvider.
When to use
Use to describe typed configuration that can be parsed from a provider or
yielded inside Effect.gen.
Details
Key members:
parse(provider, pathPrefix?) – runs the config against a specific provider.
The optional path prefix is the logical scope accumulated from outer
Config.nested calls.
- Yieldable – can be yielded inside
Effect.gen, which automatically
resolves the current ConfigProvider from the context.
- Pipeable – supports
.pipe(Config.map(...)) etc.
Config<function (type parameter) A in <A>(self: Config<A>): Config<A2 | A>A>) => interface Config<out T>A recipe for extracting a typed value T from a ConfigProvider.
When to use
Use to describe typed configuration that can be parsed from a provider or
yielded inside Effect.gen.
Details
Key members:
parse(provider, pathPrefix?) – runs the config against a specific provider.
The optional path prefix is the logical scope accumulated from outer
Config.nested calls.
- Yieldable – can be yielded inside
Effect.gen, which automatically
resolves the current ConfigProvider from the context.
- Pipeable – supports
.pipe(Config.map(...)) etc.
Config<function (type parameter) A2 in <const A2>(defaultValue: A2): <A>(self: Config<A>) => Config<A2 | A>A2 | function (type parameter) A in <A>(self: Config<A>): Config<A2 | A>A>
<function (type parameter) A in <A, const A2>(self: Config<A>, defaultValue: A2): Config<A | A2>A, const function (type parameter) A2 in <A, const A2>(self: Config<A>, defaultValue: A2): Config<A | A2>A2>(self: Config<A>(parameter) self: {
parse: (provider: ConfigProvider.ConfigProvider, pathPrefix?: Path) => Effect.Effect<T, ConfigError>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self: interface Config<out T>A recipe for extracting a typed value T from a ConfigProvider.
When to use
Use to describe typed configuration that can be parsed from a provider or
yielded inside Effect.gen.
Details
Key members:
parse(provider, pathPrefix?) – runs the config against a specific provider.
The optional path prefix is the logical scope accumulated from outer
Config.nested calls.
- Yieldable – can be yielded inside
Effect.gen, which automatically
resolves the current ConfigProvider from the context.
- Pipeable – supports
.pipe(Config.map(...)) etc.
Config<function (type parameter) A in <A, const A2>(self: Config<A>, defaultValue: A2): Config<A | A2>A>, defaultValue: const A2defaultValue: function (type parameter) A2 in <A, const A2>(self: Config<A>, defaultValue: A2): Config<A | A2>A2): interface Config<out T>A recipe for extracting a typed value T from a ConfigProvider.
When to use
Use to describe typed configuration that can be parsed from a provider or
yielded inside Effect.gen.
Details
Key members:
parse(provider, pathPrefix?) – runs the config against a specific provider.
The optional path prefix is the logical scope accumulated from outer
Config.nested calls.
- Yieldable – can be yielded inside
Effect.gen, which automatically
resolves the current ConfigProvider from the context.
- Pipeable – supports
.pipe(Config.map(...)) etc.
Config<function (type parameter) A in <A, const A2>(self: Config<A>, defaultValue: A2): Config<A | A2>A | function (type parameter) A2 in <A, const A2>(self: Config<A>, defaultValue: A2): Config<A | A2>A2>
} = dual<(...args: Array<any>) => any, <A, const A2>(self: Config<A>, defaultValue: A2) => Config<A | A2>>(arity: 2, body: <A, const A2>(self: Config<A>, defaultValue: A2) => Config<A | A2>): ((...args: Array<any>) => any) & (<A, const A2>(self: Config<A>, defaultValue: A2) => Config<A | A2>) (+1 overload)Creates a function that can be called in data-first style or data-last
(pipe-friendly) style.
When to use
Use to expose one implementation through both direct and pipe-friendly
call styles.
Details
Pass either the arity of the uncurried function or a predicate that decides
whether the current call is data-first. Arity is the common case. Use a
predicate when optional arguments make arity ambiguous.
Example (Selecting data-first or data-last style by arity)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(2, (self, that) => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Defining overloads with call signatures)
import { Function, pipe } from "effect"
const sum: {
(that: number): (self: number) => number
(self: number, that: number): number
} = Function.dual(2, (self: number, that: number): number => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Selecting data-first or data-last style with a predicate)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(
(args) => args.length === 2,
(self, that) => self + that
)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
dual(2, <function (type parameter) A in <A, const A2>(self: Config<A>, defaultValue: A2): Config<A | A2>A, const function (type parameter) A2 in <A, const A2>(self: Config<A>, defaultValue: A2): Config<A | A2>A2>(self: Config<A>(parameter) self: {
parse: (provider: ConfigProvider.ConfigProvider, pathPrefix?: Path) => Effect.Effect<T, ConfigError>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self: interface Config<out T>A recipe for extracting a typed value T from a ConfigProvider.
When to use
Use to describe typed configuration that can be parsed from a provider or
yielded inside Effect.gen.
Details
Key members:
parse(provider, pathPrefix?) – runs the config against a specific provider.
The optional path prefix is the logical scope accumulated from outer
Config.nested calls.
- Yieldable – can be yielded inside
Effect.gen, which automatically
resolves the current ConfigProvider from the context.
- Pipeable – supports
.pipe(Config.map(...)) etc.
Config<function (type parameter) A in <A, const A2>(self: Config<A>, defaultValue: A2): Config<A | A2>A>, defaultValue: const A2defaultValue: function (type parameter) A2 in <A, const A2>(self: Config<A>, defaultValue: A2): Config<A | A2>A2): interface Config<out T>A recipe for extracting a typed value T from a ConfigProvider.
When to use
Use to describe typed configuration that can be parsed from a provider or
yielded inside Effect.gen.
Details
Key members:
parse(provider, pathPrefix?) – runs the config against a specific provider.
The optional path prefix is the logical scope accumulated from outer
Config.nested calls.
- Yieldable – can be yielded inside
Effect.gen, which automatically
resolves the current ConfigProvider from the context.
- Pipeable – supports
.pipe(Config.map(...)) etc.
Config<function (type parameter) A in <A, const A2>(self: Config<A>, defaultValue: A2): Config<A | A2>A | function (type parameter) A2 in <A, const A2>(self: Config<A>, defaultValue: A2): Config<A | A2>A2> => {
return const orElse: {
<A2>(
that: (error: ConfigError) => Config<A2>
): <A>(self: Config<A>) => Config<A2 | A>
<A, A2>(
self: Config<A>,
that: (error: ConfigError) => Config<A2>
): Config<A | A2>
}
orElse(self: Config<A>(parameter) self: {
parse: (provider: ConfigProvider.ConfigProvider, pathPrefix?: Path) => Effect.Effect<T, ConfigError>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self, (err: ConfigError(parameter) err: {
_tag: 'ConfigError';
name: string;
cause: SourceError | Schema.SchemaError;
message: string;
toString: () => string;
}
err) => {
if (import SchemaSchema.function isSchemaError(u: unknown): u is SchemaErrorReturns true if u is a
SchemaError
.
isSchemaError(err: ConfigError(parameter) err: {
_tag: 'ConfigError';
name: string;
cause: SourceError | Schema.SchemaError;
message: string;
toString: () => string;
}
err.ConfigError.cause: SourceError | Schema.SchemaErrorcause)) {
const const issue: SchemaIssue.Issueissue = err: ConfigError(parameter) err: {
_tag: 'ConfigError';
name: string;
cause: SourceError | Schema.SchemaError;
message: string;
toString: () => string;
}
err.ConfigError.cause: SourceError | Schema.SchemaError(property) ConfigError.cause: {
message: string;
toString: () => string;
name: string;
stack: string;
cause: unknown;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toJSON: () => unknown;
_tag: Tag;
issue: SchemaIssue.Issue;
}
cause.issue
if (function isMissingDataOnly(
issue: SchemaIssue.Issue
): boolean
isMissingDataOnly(const issue: SchemaIssue.Issueissue)) {
return function succeed<A2>(
value: A2
): Config<A2>
Creates a config that always succeeds with the given value, ignoring the
provider entirely.
When to use
Use when you need a hardcoded config value, such as inside
orElse
or
tests.
Example (Returning a constant fallback)
import { Config } from "effect"
const host = Config.string("HOST").pipe(
Config.orElse(() => Config.succeed("localhost"))
)
succeed(defaultValue: const A2defaultValue)
}
}
return function fail(
err: SourceError | Schema.SchemaError
): Config<never>
Creates a config that always fails with the given error.
When to use
Use when you need to re-raise a specific config error, such as inside
orElse
.
fail(err: ConfigError(parameter) err: {
_tag: 'ConfigError';
name: string;
cause: SourceError | Schema.SchemaError;
message: string;
toString: () => string;
}
err.ConfigError.cause: SourceError | Schema.SchemaErrorcause)
})
})