Skip to content

PurePath.withName() method

pathlib-ts > PurePath > withName

Returns a new path with the final component replaced by the provided name.

Signature:

withName(name: string): PurePath;

Parameter

Type

Description

name

string

Replacement leaf component. Must not contain path separators.

Returns:

PurePath

A new path with the same anchor and parent but a different final component.

The Error class If the name is empty, . or contains separators.

Input validation mirrors CPython: path separators and empty names are rejected. Use this when you need to replace the leaf node while keeping the same directory.