PurePath.withSegments() method
pathlib-ts > PurePath > withSegments
PurePath.withSegments() method
Section titled “PurePath.withSegments() method”Builds a sibling path instance of the same type by combining additional segments.
Signature:
withSegments<T extends PurePath>(this: T, ...segments: Array<PathLike>): T;Parameters
Section titled “Parameters”|
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.
Remarks
Section titled “Remarks”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).
Example
Section titled “Example”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'