Encoding.ts
Encoding.ts overview
Section titled “Encoding.ts overview”Encoding and decoding helpers for Base64, Base64Url, and hexadecimal text.
The functions convert between strings, UTF-8 text, and Uint8Array bytes.
Encode functions return strings directly, while decode functions return
Result.Result so invalid input is reported as an EncodingError instead of
being thrown.
Since v4.0.0
Exports Grouped by Category
Section titled “Exports Grouped by Category”constructors
Section titled “constructors”EncodingError (class)
Section titled “EncodingError (class)”Error returned when an encoding or decoding operation cannot process its input.
When to use
Use when you need to handle or inspect failures from encoding or decoding operations.
Details
The error records whether the failure happened during encoding or decoding, which encoding module reported it, the original input, and a human-readable message.
See
isEncodingErrorfor checking whether a value is an EncodingError
Signature
declare class EncodingErrorSince v4.0.0
[EncodingErrorTypeId] (property)
Section titled “[EncodingErrorTypeId] (property)”Marks this value as an encoding or decoding error for runtime guards.
When to use
Use to identify EncodingError instances through isEncodingError.
Signature
readonly [EncodingErrorTypeId]: "~effect/encoding/EncodingError"Since v4.0.0
decoding
Section titled “decoding”decodeBase64
Section titled “decodeBase64”Decodes a base64 (RFC4648) string into bytes safely.
When to use
Use to decode a standard padded Base64 string into bytes without throwing on invalid input.
Details
Returns Result.succeed with a Uint8Array when decoding succeeds, or
Result.fail with an EncodingError when the input is not valid base64.
Example (Decoding Base64 bytes)
import { Encoding, Result } from "effect"
const result = Encoding.decodeBase64("SGVsbG8=")if (Result.isSuccess(result)) { console.log(Array.from(result.success)) // [72, 101, 108, 108, 111]}Signature
declare const decodeBase64: (str: string) => Result.Result<Uint8Array, EncodingError>Since v2.0.0
decodeBase64String
Section titled “decodeBase64String”Decodes a base64 (RFC4648) string into a UTF-8 string safely.
When to use
Use to decode a standard padded Base64 string into UTF-8 text without throwing on invalid input.
Details
Returns Result.succeed with the decoded text when decoding succeeds, or
Result.fail with an EncodingError when the input is not valid base64.
Example (Decoding Base64 strings)
import { Encoding, Result } from "effect"
const result = Encoding.decodeBase64String("aGVsbG8=")if (Result.isSuccess(result)) { console.log(result.success) // "hello"}Signature
declare const decodeBase64String: (str: string) => Result.Result<string, EncodingError>Since v2.0.0
decodeBase64Url
Section titled “decodeBase64Url”Decodes a URL-safe base64 string into bytes safely.
When to use
Use to decode padded or unpadded Base64Url text into bytes without throwing on invalid input.
Details
Returns Result.succeed with a Uint8Array when decoding succeeds, or
Result.fail with an EncodingError when the input is not valid URL-safe
base64. Both padded and unpadded URL-safe base64 forms are accepted when
otherwise valid.
Example (Decoding URL-safe Base64 bytes)
import { Encoding, Result } from "effect"
const result = Encoding.decodeBase64Url("SGVsbG8_")if (Result.isSuccess(result)) { console.log(Array.from(result.success)) // [72, 101, 108, 108, 111, 63]}Signature
declare const decodeBase64Url: (str: string) => Result.Result<Uint8Array, EncodingError>Since v2.0.0
decodeBase64UrlString
Section titled “decodeBase64UrlString”Decodes a URL-safe base64 string into a UTF-8 string safely.
When to use
Use to decode padded or unpadded Base64Url text into UTF-8 text without throwing on invalid input.
Details
Returns Result.succeed with the decoded text when decoding succeeds, or
Result.fail with an EncodingError when the input is not valid URL-safe
base64.
Example (Decoding URL-safe Base64 strings)
import { Encoding, Result } from "effect"
const result = Encoding.decodeBase64UrlString("aGVsbG8_")if (Result.isSuccess(result)) { console.log(result.success) // "hello?"}Signature
declare const decodeBase64UrlString: (str: string) => Result.Result<string, EncodingError>Since v2.0.0
decodeHex
Section titled “decodeHex”Decodes a hexadecimal string into bytes safely.
When to use
Use to decode hexadecimal text into bytes without throwing on invalid input.
Details
Returns Result.succeed with a Uint8Array when decoding succeeds, or
Result.fail with an EncodingError when the input has an odd length or
contains invalid hex characters.
Example (Decoding hex bytes)
import { Encoding, Result } from "effect"
const result = Encoding.decodeHex("48656c6c6f")if (Result.isSuccess(result)) { console.log(Array.from(result.success)) // [72, 101, 108, 108, 111]}Signature
declare const decodeHex: (str: string) => Result.Result<Uint8Array, EncodingError>Since v2.0.0
decodeHexString
Section titled “decodeHexString”Decodes a hexadecimal string into a UTF-8 string safely.
When to use
Use to decode hexadecimal text into UTF-8 text without throwing on invalid input.
Details
Returns Result.succeed with the decoded text when decoding succeeds, or
Result.fail with an EncodingError when the input is not valid hex.
Example (Decoding hex strings)
import { Encoding, Result } from "effect"
const result = Encoding.decodeHexString("68656c6c6f")if (Result.isSuccess(result)) { console.log(result.success) // "hello"}Signature
declare const decodeHexString: (str: string) => Result.Result<string, EncodingError>Since v2.0.0
encoding
Section titled “encoding”encodeBase64
Section titled “encodeBase64”Encodes the given value into a base64 (RFC4648) string.
When to use
Use to encode text or bytes as a standard padded Base64 string for storage or transport.
Details
String inputs are encoded as UTF-8 bytes before Base64 encoding.
Uint8Array inputs are encoded directly. The output uses the standard
RFC4648 alphabet with = padding.
Example (Encoding Base64 strings and bytes)
import { Encoding } from "effect"
// Encode a stringconsole.log(Encoding.encodeBase64("hello")) // "aGVsbG8="
// Encode binary dataconst bytes = new Uint8Array([72, 101, 108, 108, 111])console.log(Encoding.encodeBase64(bytes)) // "SGVsbG8="See
decodeBase64for decoding standard Base64 to bytesdecodeBase64Stringfor decoding standard Base64 to UTF-8 textencodeBase64Urlfor URL-safe unpadded Base64 output
Signature
declare const encodeBase64: (input: Uint8Array | string) => stringSince v2.0.0
encodeBase64Url
Section titled “encodeBase64Url”Encodes the given value into a base64 (URL) string.
When to use
Use to encode text or bytes as an unpadded Base64Url string for contexts that require the URL-safe alphabet.
Details
String inputs are encoded as UTF-8 bytes before Base64Url encoding.
Uint8Array inputs are encoded directly. The output removes = padding and
replaces + with - and / with _.
Example (Encoding URL-safe Base64)
import { Encoding } from "effect"
// URL-safe base64 encoding (uses - and _ instead of + and /)console.log(Encoding.encodeBase64Url("hello?")) // "aGVsbG8_"
const bytes = new Uint8Array([72, 101, 108, 108, 111, 63])console.log(Encoding.encodeBase64Url(bytes)) // "SGVsbG8_"See
decodeBase64Urlfor decoding URL-safe Base64 to bytesdecodeBase64UrlStringfor decoding URL-safe Base64 to UTF-8 textencodeBase64for standard padded Base64 output
Signature
declare const encodeBase64Url: (input: Uint8Array | string) => stringSince v2.0.0
encodeHex
Section titled “encodeHex”Encodes the given value into a hex string.
When to use
Use to encode text or bytes as lowercase hexadecimal text.
Example (Encoding hex strings and bytes)
import { Encoding } from "effect"
// Encode a string to hexconsole.log(Encoding.encodeHex("hello")) // "68656c6c6f"
// Encode binary data to hexconst bytes = new Uint8Array([72, 101, 108, 108, 111])console.log(Encoding.encodeHex(bytes)) // "48656c6c6f"Signature
declare const encodeHex: (input: Uint8Array | string) => stringSince v2.0.0
guards
Section titled “guards”isEncodingError
Section titled “isEncodingError”Checks whether a value is an EncodingError.
When to use
Use to narrow an unknown value before handling it as an EncodingError from
encoding or decoding code.
Details
Returns true when the value carries the EncodingErrorTypeId marker and
narrows the value to EncodingError.
See
EncodingErrorfor the structured error produced by failed encoding and decoding operations
Signature
declare const isEncodingError: (u: unknown) => u is EncodingErrorSince v4.0.0
type IDs
Section titled “type IDs”EncodingErrorTypeId
Section titled “EncodingErrorTypeId”Type identifier stored on EncodingError values and used by
isEncodingError.
When to use
Use when implementing low-level EncodingError-compatible values that need
to carry the runtime marker.
Details
This marker is part of the runtime representation of EncodingError. Prefer
isEncodingError when narrowing unknown values.
See
isEncodingErrorfor the public guard that checks this marker
Signature
declare const EncodingErrorTypeId: "~effect/encoding/EncodingError"Since v4.0.0
EncodingErrorTypeId (type alias)
Section titled “EncodingErrorTypeId (type alias)”Literal type of the EncodingErrorTypeId marker.
When to use
Use to type the marker carried by EncodingError values.
Signature
type EncodingErrorTypeId = typeof EncodingErrorTypeIdSince v4.0.0