(
impl: Omit<
FileSystem,
| typeof TypeId
| "exists"
| "readFileString"
| "stream"
| "sink"
| "writeFileString"
>
): FileSystemCreates a FileSystem implementation from a partial implementation.
When to use
Use to build a concrete FileSystem service from platform-specific core
operations while deriving the convenience methods that can be implemented
from them.
Details
This function takes a partial FileSystem implementation and automatically provides
default implementations for exists, readFileString, stream, sink, and
writeFileString methods based on the provided core methods.
export const const make: (
impl: Omit<
FileSystem,
| typeof TypeId
| "exists"
| "readFileString"
| "stream"
| "sink"
| "writeFileString"
>
) => FileSystem
Creates a FileSystem implementation from a partial implementation.
When to use
Use to build a concrete FileSystem service from platform-specific core
operations while deriving the convenience methods that can be implemented
from them.
Details
This function takes a partial FileSystem implementation and automatically provides
default implementations for exists, readFileString, stream, sink, and
writeFileString methods based on the provided core methods.
make = (
impl: Omit<
FileSystem,
| typeof TypeId
| "exists"
| "readFileString"
| "stream"
| "sink"
| "writeFileString"
>
impl: type Omit<T, K extends keyof any> = {
[P in Exclude<keyof T, K>]: T[P]
}
Construct a type with the properties of T except for those in type K.
Omit<FileSystem, typeof const TypeId: "~effect/platform/FileSystem"TypeId | "exists" | "readFileString" | "stream" | "sink" | "writeFileString">
): FileSystem =>
const FileSystem: Context.Service<
FileSystem,
FileSystem
>
const FileSystem: {
key: string;
Service: {
access: (path: string, options?: { readonly ok?: boolean | undefined; readonly readable?: boolean | undefined; readonly writable?: boolean | undefined }) => Effect.Effect<void, PlatformError>;
copy: (fromPath: string, toPath: string, options?: { readonly overwrite?: boolean | undefined; readonly preserveTimestamps?: boolean | undefined }) => Effect.Effect<void, PlatformError>;
copyFile: (fromPath: string, toPath: string) => Effect.Effect<void, PlatformError>;
chmod: (path: string, mode: number) => Effect.Effect<void, PlatformError>;
chown: (path: string, uid: number, gid: number) => Effect.Effect<void, PlatformError>;
glob: (pattern: string, options?: { readonly root?: string | undefined; readonly exclude?: ReadonlyArray<string> | undefined }) => Effect.Effect<Array<string>, PlatformError>;
exists: (path: string) => Effect.Effect<boolean, PlatformError>;
link: (fromPath: string, toPath: string) => Effect.Effect<void, PlatformError>;
makeDirectory: (path: string, options?: { readonly recursive?: boolean | undefined; readonly mode?: number | undefined }) => Effect.Effect<void, PlatformError>;
makeTempDirectory: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined }) => Effect.Effect<string, PlatformError>;
makeTempDirectoryScoped: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined }) => Effect.Effect<string, PlatformError, Scope>;
makeTempFile: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined; readonly suffix?: string | undefined }) => Effect.Effect<string, PlatformError>;
makeTempFileScoped: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined; readonly suffix?: string | undefined }) => Effect.Effect<string, PlatformError, Scope>;
open: (path: string, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Effect.Effect<File, PlatformError, Scope>;
readDirectory: (path: string, options?: { readonly recursive?: boolean | undefined }) => Effect.Effect<Array<string>, PlatformError>;
readFile: (path: string) => Effect.Effect<Uint8Array, PlatformError>;
readFileString: (path: string, encoding?: string) => Effect.Effect<string, PlatformError>;
readLink: (path: string) => Effect.Effect<string, PlatformError>;
realPath: (path: string) => Effect.Effect<string, PlatformError>;
remove: (path: string, options?: { readonly recursive?: boolean | undefined; readonly force?: boolean | undefined }) => Effect.Effect<void, PlatformError>;
rename: (oldPath: string, newPath: string) => Effect.Effect<void, PlatformError>;
sink: (path: string, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Sink.Sink<void, Uint8Array, never, PlatformError>;
stat: (path: string) => Effect.Effect<File.Info, PlatformError>;
stream: (path: string, options?: { readonly bytesToRead?: SizeInput | undefined; readonly chunkSize?: SizeInput | undefined; readonly offset?: SizeInput | undefined }) => Stream.Stream<Uint8Array, PlatformError>;
symlink: (fromPath: string, toPath: string) => Effect.Effect<void, PlatformError>;
truncate: (path: string, length?: SizeInput) => Effect.Effect<void, PlatformError>;
utimes: (path: string, atime: Date | number, mtime: Date | number) => Effect.Effect<void, PlatformError>;
watch: (path: string) => Stream.Stream<WatchEvent, PlatformError>;
writeFile: (path: string, data: Uint8Array, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Effect.Effect<void, PlatformError>;
writeFileString: (path: string, data: string, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Effect.Effect<void, PlatformError>;
};
of: (this: void, self: FileSystem) => FileSystem;
context: (self: FileSystem) => Context.Context<FileSystem>;
use: (f: (service: FileSystem) => Effect.Effect<A, E, R>) => Effect.Effect<A, E, FileSystem | R>;
useSync: (f: (service: FileSystem) => A) => Effect.Effect<A, never, FileSystem>;
Identifier: Identifier;
stack: string | undefined;
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;
}
Core interface for file system operations in Effect.
Details
The FileSystem interface provides a comprehensive set of file and directory operations
that work cross-platform. All operations return Effect values that can be composed,
transformed, and executed safely with proper error handling.
Example (Accessing file system operations)
import { Console, Effect, FileSystem } from "effect"
const program = Effect.gen(function*() {
const fs = yield* FileSystem.FileSystem
// Basic file operations
const exists = yield* fs.exists("./config.json")
if (!exists) {
yield* fs.writeFileString("./config.json", "{\"env\": \"development\"}")
}
// Directory operations
yield* fs.makeDirectory("./logs", { recursive: true })
// File information
const stats = yield* fs.stat("./config.json")
yield* Console.log(`File size: ${stats.size} bytes`)
// Streaming operations
const content = yield* fs.readFileString("./config.json")
yield* Console.log("Config:", content)
})
Service tag for platform file-system operations.
When to use
Use to access or provide operations for files, directories, permissions,
streams, and sinks through the Effect context.
Details
This key is used to provide and access the FileSystem service in the Effect context.
Example (Accessing and providing FileSystem)
import { Effect, FileSystem } from "effect"
// Access the FileSystem service
const program = Effect.gen(function*() {
const fs = yield* FileSystem.FileSystem
const exists = yield* fs.exists("./data.txt")
if (exists) {
const content = yield* fs.readFileString("./data.txt")
yield* Effect.log("File content:", content)
}
})
// Provide a custom FileSystem implementation
declare const platformImpl: Omit<
FileSystem.FileSystem,
"exists" | "readFileString" | "stream" | "sink" | "writeFileString"
>
const customFs = FileSystem.make(platformImpl)
const withCustomFs = Effect.provideService(
program,
FileSystem.FileSystem,
customFs
)
FileSystem.of({
...impl: Omit<
FileSystem,
| typeof TypeId
| "exists"
| "readFileString"
| "stream"
| "sink"
| "writeFileString"
>
impl,
[const TypeId: "~effect/platform/FileSystem"TypeId]: const TypeId: "~effect/platform/FileSystem"TypeId,
exists: (path: any) => neverexists: (path: anypath) =>
pipe<Effect.Effect<void, PlatformError>, never, never>(a: Effect.Effect<void, PlatformError>, ab: (a: Effect.Effect<void, PlatformError>) => never, bc: (b: never) => never): never (+19 overloads)Pipes the value of an expression through a left-to-right sequence of
functions.
When to use
Use when you need to compose data-last functions into readable
transformation pipelines instead of method-style chains.
Details
Takes an initial value, passes it to the first function, then passes each
result to the next function in order. The final function result is returned.
Gotchas
Each function passed after the initial value must accept a single argument,
because pipe calls each step with only the previous result.
Example (Piping values through functions)
In this example, 1 is passed to the first function, and each result becomes
the input for the next function.
import { pipe } from "effect"
const result = pipe(
1,
(n) => n + 1,
(n) => n * 2,
(n) => `result: ${n}`
)
console.log(result) // "result: 4"
Example (Chaining methods before conversion)
const numbers = [1, 2, 3, 4]
const double = (n: number) => n * 2
const greaterThanFour = (n: number) => n > 4
const result = numbers.map(double).filter(greaterThanFour)
console.log(result) // [6, 8]
Example (Rewriting method chains with pipe)
The same transformation can be written with data-last functions.
import { Array, pipe } from "effect"
const numbers = [1, 2, 3, 4]
const double = (n: number) => n * 2
const greaterThanFour = (n: number) => n > 4
const result = pipe(
numbers,
Array.map(double),
Array.filter(greaterThanFour)
)
console.log(result) // [6, 8]
Example (Chaining arithmetic operations)
import { pipe } from "effect"
// Define simple arithmetic operations
const increment = (x: number) => x + 1
const double = (x: number) => x * 2
const subtractTen = (x: number) => x - 10
// Sequentially apply these operations using `pipe`
const result = pipe(5, increment, double, subtractTen)
console.log(result)
// Output: 2
Example (Building a simple transformation pipeline)
import { pipe } from "effect"
// Simple transformation pipeline
const result = pipe(
5,
(x) => x * 2, // 10
(x) => x + 1, // 11
(x) => x.toString() // "11"
)
console.log(result) // "11"
pipe(
impl: Omit<
FileSystem,
| typeof TypeId
| "exists"
| "readFileString"
| "stream"
| "sink"
| "writeFileString"
>
impl.access: (
path: string,
options?: {
readonly ok?: boolean | undefined
readonly readable?: boolean | undefined
readonly writable?: boolean | undefined
}
) => Effect.Effect<void, PlatformError>
Checks whether a file can be accessed.
You can optionally specify the level of access to check for.
access(path: anypath),
import EffectEffect.as(true),
import EffectEffect.catchTag(
"PlatformError",
(e: PlatformError(parameter) e: {
message: 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; <…;
toString: () => string;
toJSON: () => unknown;
_tag: Tag;
reason: BadArgument | SystemError;
}
e) => e: PlatformError(parameter) e: {
message: 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; <…;
toString: () => string;
toJSON: () => unknown;
_tag: Tag;
reason: BadArgument | SystemError;
}
e.reason._tag === "NotFound" ? import EffectEffect.succeed(false) : import EffectEffect.fail(e: PlatformError(parameter) e: {
message: 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; <…;
toString: () => string;
toJSON: () => unknown;
_tag: Tag;
reason: BadArgument | SystemError;
}
e)
)
),
readFileString: (path: any, encoding: any) => anyreadFileString: (path: anypath, encoding: anyencoding) =>
import EffectEffect.flatMap(impl: Omit<
FileSystem,
| typeof TypeId
| "exists"
| "readFileString"
| "stream"
| "sink"
| "writeFileString"
>
impl.readFile: (
path: string
) => Effect.Effect<Uint8Array, PlatformError>
Read the contents of a file.
readFile(path: anypath), (_: any_) =>
import EffectEffect.try({
try: () => stringtry: () => new var TextDecoder: new (
label?: string,
options?: TextDecoderOptions
) => TextDecoder
The TextDecoder interface represents a decoder for a specific text encoding, such as UTF-8, ISO-8859-2, KOI8-R, GBK, etc.
TextDecoder(encoding: anyencoding).TextDecoder.decode(input?: AllowSharedBufferSource, options?: TextDecodeOptions): stringThe TextDecoder.decode() method returns a string containing text decoded from the buffer passed as a parameter.
decode(_: any_),
catch: (cause: any) => PlatformErrorcatch: (cause: anycause) =>
function badArgument(options: { readonly module: string; readonly method: string; readonly description?: string | undefined; readonly cause?: unknown }): PlatformErrorCreates a PlatformError whose reason is a BadArgument.
When to use
Use to report a platform API rejecting caller input before performing the
underlying operation.
badArgument({
module: stringmodule: "FileSystem",
method: stringmethod: "readFileString",
description?: string | undefineddescription: "invalid encoding",
cause?: unknowncause
})
})),
stream: (
path: string,
options:
| {
readonly bytesToRead?:
| SizeInput
| undefined
readonly chunkSize?: SizeInput | undefined
readonly offset?: SizeInput | undefined
}
| undefined
) => Stream.Stream<
Uint8Array<ArrayBufferLike>,
PlatformError,
never
>
stream: import EffectEffect.fnUntraced(function*(path: anypath, options: | {
readonly bytesToRead?: SizeInput | undefined
readonly chunkSize?: SizeInput | undefined
readonly offset?: SizeInput | undefined
}
| undefined
options) {
const const file: Fileconst file: {
fd: File.Descriptor;
stat: Effect.Effect<File.Info, PlatformError>;
seek: (offset: SizeInput, from: SeekMode) => Effect.Effect<void>;
sync: Effect.Effect<void, PlatformError>;
read: (buffer: Uint8Array) => Effect.Effect<Size, PlatformError>;
readAlloc: (size: SizeInput) => Effect.Effect<Option.Option<Uint8Array>, PlatformError>;
truncate: (length?: SizeInput) => Effect.Effect<void, PlatformError>;
write: (buffer: Uint8Array) => Effect.Effect<Size, PlatformError>;
writeAll: (buffer: Uint8Array) => Effect.Effect<void, PlatformError>;
}
file = yield* impl: Omit<
FileSystem,
| typeof TypeId
| "exists"
| "readFileString"
| "stream"
| "sink"
| "writeFileString"
>
impl.open: (
path: string,
options?: {
readonly flag?: OpenFlag | undefined
readonly mode?: number | undefined
}
) => Effect.Effect<File, PlatformError, Scope>
Open a file at path with the specified options.
Details
The file handle will be automatically closed when the scope is closed.
open(path: anypath, { flag?: OpenFlag | undefinedflag: "r" })
if (options: | {
readonly bytesToRead?: SizeInput | undefined
readonly chunkSize?: SizeInput | undefined
readonly offset?: SizeInput | undefined
}
| undefined
options?.offset) {
yield* const file: Fileconst file: {
fd: File.Descriptor;
stat: Effect.Effect<File.Info, PlatformError>;
seek: (offset: SizeInput, from: SeekMode) => Effect.Effect<void>;
sync: Effect.Effect<void, PlatformError>;
read: (buffer: Uint8Array) => Effect.Effect<Size, PlatformError>;
readAlloc: (size: SizeInput) => Effect.Effect<Option.Option<Uint8Array>, PlatformError>;
truncate: (length?: SizeInput) => Effect.Effect<void, PlatformError>;
write: (buffer: Uint8Array) => Effect.Effect<Size, PlatformError>;
writeAll: (buffer: Uint8Array) => Effect.Effect<void, PlatformError>;
}
file.seek(options: {
readonly bytesToRead?: SizeInput | undefined
readonly chunkSize?: SizeInput | undefined
readonly offset?: SizeInput | undefined
}
(parameter) options: {
bytesToRead: SizeInput | undefined;
chunkSize: SizeInput | undefined;
offset: SizeInput | undefined;
}
options.offset, "start")
}
const const bytesToRead: anybytesToRead = options: | {
readonly bytesToRead?: SizeInput | undefined
readonly chunkSize?: SizeInput | undefined
readonly offset?: SizeInput | undefined
}
| undefined
options?.bytesToRead !== var undefinedundefined ? const Size: (bytes: SizeInput) => SizeCreates a Size from various numeric input types.
Details
Converts numbers, bigints, or existing Size values into a properly
branded Size type. This function handles the conversion and ensures
type safety for file size operations.
Example (Converting size inputs)
import { Effect, FileSystem } from "effect"
// From number
const size1 = FileSystem.Size(1024)
console.log(typeof size1) // "bigint"
// From bigint
const size2 = FileSystem.Size(BigInt(2048))
// From existing Size (identity)
const size3 = FileSystem.Size(size1)
// Use in file operations
const readChunk = (path: string, chunkSize: number) =>
Effect.gen(function*() {
const fs = yield* FileSystem.FileSystem
return fs.stream(path, {
chunkSize: FileSystem.Size(chunkSize)
})
})
Size(options: {
readonly bytesToRead?: SizeInput | undefined
readonly chunkSize?: SizeInput | undefined
readonly offset?: SizeInput | undefined
}
(parameter) options: {
bytesToRead: SizeInput | undefined;
chunkSize: SizeInput | undefined;
offset: SizeInput | undefined;
}
options.bytesToRead) : var undefinedundefined
let let totalBytesRead: biginttotalBytesRead = var BigInt: BigIntConstructor
;(value: bigint | boolean | number | string) =>
bigint
BigInt(0)
const const chunkSize: SizechunkSize = const Size: (bytes: SizeInput) => SizeCreates a Size from various numeric input types.
Details
Converts numbers, bigints, or existing Size values into a properly
branded Size type. This function handles the conversion and ensures
type safety for file size operations.
Example (Converting size inputs)
import { Effect, FileSystem } from "effect"
// From number
const size1 = FileSystem.Size(1024)
console.log(typeof size1) // "bigint"
// From bigint
const size2 = FileSystem.Size(BigInt(2048))
// From existing Size (identity)
const size3 = FileSystem.Size(size1)
// Use in file operations
const readChunk = (path: string, chunkSize: number) =>
Effect.gen(function*() {
const fs = yield* FileSystem.FileSystem
return fs.stream(path, {
chunkSize: FileSystem.Size(chunkSize)
})
})
Size(options: | {
readonly bytesToRead?: SizeInput | undefined
readonly chunkSize?: SizeInput | undefined
readonly offset?: SizeInput | undefined
}
| undefined
options?.chunkSize ?? 64 * 1024)
const const readChunk: Effect.Effect<
Option.Option<Uint8Array<ArrayBufferLike>>,
PlatformError,
never
>
const readChunk: {
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;
}
readChunk = const file: Fileconst file: {
fd: File.Descriptor;
stat: Effect.Effect<File.Info, PlatformError>;
seek: (offset: SizeInput, from: SeekMode) => Effect.Effect<void>;
sync: Effect.Effect<void, PlatformError>;
read: (buffer: Uint8Array) => Effect.Effect<Size, PlatformError>;
readAlloc: (size: SizeInput) => Effect.Effect<Option.Option<Uint8Array>, PlatformError>;
truncate: (length?: SizeInput) => Effect.Effect<void, PlatformError>;
write: (buffer: Uint8Array) => Effect.Effect<Size, PlatformError>;
writeAll: (buffer: Uint8Array) => Effect.Effect<void, PlatformError>;
}
file.readAlloc(const chunkSize: SizechunkSize)
return import StreamStream.const fromPull: <A, E, R, EX, RX>(
pull: Effect.Effect<
Pull.Pull<
Arr.NonEmptyReadonlyArray<A>,
E,
void,
R
>,
EX,
RX
>
) => Stream<A, Pull.ExcludeDone<E> | EX, R | RX>
Creates a stream from a pull effect, such as one produced by Stream.toPull.
Details
A pull effect yields chunks on demand and completes when the upstream stream ends.
See Stream.toPull for a matching producer.
Example (Creating a stream from a pull effect)
import { Console, Effect, Stream } from "effect"
const program = Effect.scoped(
Effect.gen(function*() {
const source = Stream.make(1, 2, 3)
const pull = yield* Stream.toPull(source)
const stream = Stream.fromPull(Effect.succeed(pull))
const values = yield* Stream.runCollect(stream)
yield* Console.log(values)
})
)
Effect.runPromise(program)
// Output: [1, 2, 3]
fromPull(import EffectEffect.succeed(
import EffectEffect.flatMap(
import EffectEffect.suspend((): import PullPull.interface Pull<out A, out E = never, out Done = void, out R = never>An effectful pull step that either produces a value, fails with E, or
signals completion with Cause.Done<Done>.
When to use
Use to model one low-level pull step when a consumer repeatedly evaluates an
effect that may emit a value, fail normally, or signal normal completion
through Cause.Done.
Details
Pull represents completion in the error channel so low-level stream
consumers can distinguish ordinary failures from end-of-input and carry a
leftover value when needed.
Pull<import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<interface Uint8Array<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike>A typed array of 8-bit unsigned integer values. The contents are initialized to 0. If the
requested number of bytes could not be allocated an exception is raised.
Uint8Array>, class PlatformErrorclass PlatformError {
message: 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; <…;
toString: () => string;
toJSON: () => unknown;
_tag: Tag;
reason: BadArgument | SystemError;
}
Tagged error used by platform APIs to report either invalid arguments or
system-level failures.
When to use
Use as the shared error type for platform APIs that expose invalid arguments
and host or operating-system failures through a single Effect error
channel.
Details
The reason field contains the underlying BadArgument or SystemError.
When that reason has a cause, the cause is preserved on the wrapper.
PlatformError> => {
if (const bytesToRead: anybytesToRead !== var undefinedundefined && const bytesToRead: SizebytesToRead <= let totalBytesRead: biginttotalBytesRead) {
return import CauseCause.done()
}
return const bytesToRead: anybytesToRead !== var undefinedundefined && (const bytesToRead: SizebytesToRead - let totalBytesRead: biginttotalBytesRead) < const chunkSize: SizechunkSize
? const file: Fileconst file: {
fd: File.Descriptor;
stat: Effect.Effect<File.Info, PlatformError>;
seek: (offset: SizeInput, from: SeekMode) => Effect.Effect<void>;
sync: Effect.Effect<void, PlatformError>;
read: (buffer: Uint8Array) => Effect.Effect<Size, PlatformError>;
readAlloc: (size: SizeInput) => Effect.Effect<Option.Option<Uint8Array>, PlatformError>;
truncate: (length?: SizeInput) => Effect.Effect<void, PlatformError>;
write: (buffer: Uint8Array) => Effect.Effect<Size, PlatformError>;
writeAll: (buffer: Uint8Array) => Effect.Effect<void, PlatformError>;
}
file.readAlloc(const bytesToRead: SizebytesToRead - let totalBytesRead: biginttotalBytesRead)
: const readChunk: Effect.Effect<
Option.Option<Uint8Array<ArrayBufferLike>>,
PlatformError,
never
>
const readChunk: {
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;
}
readChunk
}),
import OptionOption.const match: {
<B, A, C = B>(options: {
readonly onNone: LazyArg<B>
readonly onSome: (a: A) => C
}): (self: Option<A>) => B | C
<A, B, C = B>(
self: Option<A>,
options: {
readonly onNone: LazyArg<B>
readonly onSome: (a: A) => C
}
): B | C
}
match({
onNone: LazyArg<any>onNone: () => import CauseCause.done(),
onSome: (a: unknown) => anyonSome: (buf: unknownbuf) => {
let totalBytesRead: biginttotalBytesRead += var BigInt: BigIntConstructor
;(value: bigint | boolean | number | string) =>
bigint
BigInt(buf: unknownbuf.length)
return import EffectEffect.succeed(import ArrArr.of(buf: unknownbuf))
}
})
)
))
}, import StreamStream.const unwrap: <A, E2, R2, E, R>(
effect: Effect.Effect<Stream<A, E2, R2>, E, R>
) => Stream<
A,
E | E2,
R2 | Exclude<R, Scope.Scope>
>
Creates a stream produced from an Effect.
Example (Unwrapping a stream effect)
import { Console, Effect, Stream } from "effect"
const effect = Effect.succeed(Stream.make(1, 2, 3))
const stream = Stream.unwrap(effect)
const program = Effect.gen(function*() {
const chunk = yield* Stream.runCollect(stream)
yield* Console.log(chunk)
})
// [1, 2, 3]
unwrap),
sink: (
path: string,
options:
| {
readonly flag?: OpenFlag | undefined
readonly mode?: number | undefined
}
| undefined
) => Sink.Sink<
void,
Uint8Array<ArrayBufferLike>,
never,
PlatformError,
never
>
sink: (path: anypath, options: | {
readonly flag?: OpenFlag | undefined
readonly mode?: number | undefined
}
| undefined
options) =>
pipe<Effect.Effect<File, PlatformError, Scope>, never, Sink.Sink<unknown, unknown, unknown, unknown, unknown>>(a: Effect.Effect<File, PlatformError, Scope>, ab: (a: Effect.Effect<File, PlatformError, Scope>) => never, bc: (b: never) => Sink.Sink<unknown, unknown, unknown, unknown, unknown>): Sink.Sink<unknown, unknown, unknown, unknown, unknown> (+19 overloads)Pipes the value of an expression through a left-to-right sequence of
functions.
When to use
Use when you need to compose data-last functions into readable
transformation pipelines instead of method-style chains.
Details
Takes an initial value, passes it to the first function, then passes each
result to the next function in order. The final function result is returned.
Gotchas
Each function passed after the initial value must accept a single argument,
because pipe calls each step with only the previous result.
Example (Piping values through functions)
In this example, 1 is passed to the first function, and each result becomes
the input for the next function.
import { pipe } from "effect"
const result = pipe(
1,
(n) => n + 1,
(n) => n * 2,
(n) => `result: ${n}`
)
console.log(result) // "result: 4"
Example (Chaining methods before conversion)
const numbers = [1, 2, 3, 4]
const double = (n: number) => n * 2
const greaterThanFour = (n: number) => n > 4
const result = numbers.map(double).filter(greaterThanFour)
console.log(result) // [6, 8]
Example (Rewriting method chains with pipe)
The same transformation can be written with data-last functions.
import { Array, pipe } from "effect"
const numbers = [1, 2, 3, 4]
const double = (n: number) => n * 2
const greaterThanFour = (n: number) => n > 4
const result = pipe(
numbers,
Array.map(double),
Array.filter(greaterThanFour)
)
console.log(result) // [6, 8]
Example (Chaining arithmetic operations)
import { pipe } from "effect"
// Define simple arithmetic operations
const increment = (x: number) => x + 1
const double = (x: number) => x * 2
const subtractTen = (x: number) => x - 10
// Sequentially apply these operations using `pipe`
const result = pipe(5, increment, double, subtractTen)
console.log(result)
// Output: 2
Example (Building a simple transformation pipeline)
import { pipe } from "effect"
// Simple transformation pipeline
const result = pipe(
5,
(x) => x * 2, // 10
(x) => x + 1, // 11
(x) => x.toString() // "11"
)
console.log(result) // "11"
pipe(
impl: Omit<
FileSystem,
| typeof TypeId
| "exists"
| "readFileString"
| "stream"
| "sink"
| "writeFileString"
>
impl.open: (
path: string,
options?: {
readonly flag?: OpenFlag | undefined
readonly mode?: number | undefined
}
) => Effect.Effect<File, PlatformError, Scope>
Open a file at path with the specified options.
Details
The file handle will be automatically closed when the scope is closed.
open(path: anypath, { flag?: OpenFlag | undefinedflag: "w", ...options: | {
readonly flag?: OpenFlag | undefined
readonly mode?: number | undefined
}
| undefined
options }),
import EffectEffect.map((file: File(parameter) file: {
fd: File.Descriptor;
stat: Effect.Effect<File.Info, PlatformError>;
seek: (offset: SizeInput, from: SeekMode) => Effect.Effect<void>;
sync: Effect.Effect<void, PlatformError>;
read: (buffer: Uint8Array) => Effect.Effect<Size, PlatformError>;
readAlloc: (size: SizeInput) => Effect.Effect<Option.Option<Uint8Array>, PlatformError>;
truncate: (length?: SizeInput) => Effect.Effect<void, PlatformError>;
write: (buffer: Uint8Array) => Effect.Effect<Size, PlatformError>;
writeAll: (buffer: Uint8Array) => Effect.Effect<void, PlatformError>;
}
file) => import SinkSink.const forEach: <In, X, E, R>(
f: (input: In) => Effect.Effect<X, E, R>
) => Sink<void, In, never, E, R>
A sink that executes the provided effectful function for every item fed
to it.
Example (Running effects for each item)
import { Console, Effect, Sink, Stream } from "effect"
// Create a sink that logs each item
const sink = Sink.forEach((item: number) => Console.log(`Processing: ${item}`))
// Use it with a stream
const stream = Stream.make(1, 2, 3)
const program = Stream.run(stream, sink)
Effect.runPromise(program)
// Output:
// Processing: 1
// Processing: 2
// Processing: 3
forEach((_: Uint8Array<ArrayBufferLike>_: interface Uint8Array<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike>A typed array of 8-bit unsigned integer values. The contents are initialized to 0. If the
requested number of bytes could not be allocated an exception is raised.
Uint8Array) => file: File(parameter) file: {
fd: File.Descriptor;
stat: Effect.Effect<File.Info, PlatformError>;
seek: (offset: SizeInput, from: SeekMode) => Effect.Effect<void>;
sync: Effect.Effect<void, PlatformError>;
read: (buffer: Uint8Array) => Effect.Effect<Size, PlatformError>;
readAlloc: (size: SizeInput) => Effect.Effect<Option.Option<Uint8Array>, PlatformError>;
truncate: (length?: SizeInput) => Effect.Effect<void, PlatformError>;
write: (buffer: Uint8Array) => Effect.Effect<Size, PlatformError>;
writeAll: (buffer: Uint8Array) => Effect.Effect<void, PlatformError>;
}
file.writeAll(_: Uint8Array<ArrayBufferLike>_))),
import SinkSink.const unwrap: <A, In, L, E, R, R2>(
effect: Effect.Effect<
Sink<A, In, L, E, R2>,
E,
R
>
) => Sink<
A,
In,
L,
E,
Exclude<R, Scope.Scope> | R2
>
Creates a sink produced from a scoped effect.
Example (Unwrapping a sink effect)
import { Console, Effect, Sink, Stream } from "effect"
// Create a sink from an effect that produces a sink
const sinkEffect = Effect.succeed(
Sink.forEach((item: number) => Console.log(`Item: ${item}`))
)
const sink = Sink.unwrap(sinkEffect)
// Use it with a stream
const stream = Stream.make(1, 2, 3)
const program = Stream.run(stream, sink)
Effect.runPromise(program)
// Output:
// Item: 1
// Item: 2
// Item: 3
unwrap
),
writeFileString: (
path: string,
data: string,
options:
| {
readonly flag?: OpenFlag | undefined
readonly mode?: number | undefined
}
| undefined
) => Effect.Effect<void, PlatformError, never>
writeFileString: (path: anypath, data: anydata, options: | {
readonly flag?: OpenFlag | undefined
readonly mode?: number | undefined
}
| undefined
options) =>
import EffectEffect.flatMap(
import EffectEffect.try({
try: () => Uint8Array<ArrayBuffer>try: () => new var TextEncoder: new () => TextEncoderThe TextEncoder interface takes a stream of code points as input and emits a stream of UTF-8 bytes.
TextEncoder().TextEncoder.encode(input?: string): Uint8Array<ArrayBuffer>The TextEncoder.encode() method takes a string as input, and returns a Global_Objects/Uint8Array containing the text given in parameters encoded with the specific method for that TextEncoder object.
encode(data: anydata),
catch: (cause: any) => PlatformErrorcatch: (cause: anycause) =>
function badArgument(options: { readonly module: string; readonly method: string; readonly description?: string | undefined; readonly cause?: unknown }): PlatformErrorCreates a PlatformError whose reason is a BadArgument.
When to use
Use to report a platform API rejecting caller input before performing the
underlying operation.
badArgument({
module: stringmodule: "FileSystem",
method: stringmethod: "writeFileString",
description?: string | undefineddescription: "could not encode string",
cause?: unknowncause
})
}),
(_: any_) => impl: Omit<
FileSystem,
| typeof TypeId
| "exists"
| "readFileString"
| "stream"
| "sink"
| "writeFileString"
>
impl.writeFile: (
path: string,
data: Uint8Array,
options?: {
readonly flag?: OpenFlag | undefined
readonly mode?: number | undefined
}
) => Effect.Effect<void, PlatformError>
Write data to a file at path.
writeFile(path: anypath, _: any_, options: | {
readonly flag?: OpenFlag | undefined
readonly mode?: number | undefined
}
| undefined
options)
)
})