Skip to content

PurePath.withSegments() method

pathlib-ts > PurePath > withSegments

Builds a sibling path instance of the same type by combining additional segments.

Signature:

withSegments<T extends PurePath>(this: T, ...segments: Array<PathLike>): T;

Parameter

Type

Description

this

T

segments

Array<PathLike>

Additional path fragments to combine. Absolute or anchored segments replace the accumulated path, mirroring CPython semantics.

Returns:

T

A new path instance of the same concrete type.

This helper powers APIs such as PurePath.joinpath() and directory iteration. Override it in subclasses when you need to preserve extra metadata on derived paths (for example, custom session identifiers).

Creating a child path while preserving subclass state

import { PurePath } from "pathlib-ts";
class TaggedPath extends PurePath {
constructor(tag: string, ...segments: Array<string>) {
super(...segments);
this.tag = tag;
}
tag: string;
}
const root = new TaggedPath("build", "./dist");
const asset = root.withSegments(root, "scripts", "main.js");
console.log(asset.toString()); // 'dist/scripts/main.js'