Creating a Branded Type for Validated File Path Using Effects

I would like to create a branded type for a validated file path (maybe terrible idea 😅 ).
For this, it looks like I need to use an effect (fs.exists) in the first argument of Brand.refined (which expects a simple predicate).
Would this be the correct way?
import { FileSystem } from "@effect/platform";
import { NodeContext } from "@effect/platform-node";
import { Brand, Effect, pipe } from "effect";

type FilePath = string & Brand.Brand<"FilePath">;

const FilePath = Brand.refined<FilePath>(
  (fp) =>
    pipe(
      Effect.gen(function* () {
        const fs = yield* FileSystem.FileSystem;
        return yield* fs.exists(fp);
      }),
      Effect.orElseFail(() => false),
      Effect.provide(NodeContext.layer),
      Effect.runSync,
    ),
  (fp) => Brand.error(`The provided path ${fp} doesn't exist`),
);
Was this page helpful?