Skip to content
Effect Days 2026 Get your ticket

IpNetwork

Pure, canonical IPv4 and IPv6 network prefixes using CIDR notation.

22 exports Added in v4.0.0 Source

Constructors

fromAddress

Added in v4.0.0 Source

Creates the canonical network containing an address by clearing its host bits.

Gotchas

The returned network's address can differ from the input address.

Signature

declare function fromAddress<A extends IpAddress>(address: A, prefixLength: number): Result<IpNetwork<A>, NetAddressError>

make

Added in v4.0.0 Source

Creates a network prefix when the address has no host bits set.

Gotchas

Use fromAddress when a host address should be truncated to its containing network.

Signature

declare function make<A extends IpAddress>(address: A, prefixLength: number): Result<IpNetwork<A>, NetAddressError>

Conversions

Returns the canonical network containing an interface address.

Signature

declare function fromInterface<A extends IpAddress>(self: IpInterface<A>): IpNetwork<A>

Decoding

fromString

Added in v4.0.0 Source

Parses a strict IPv4 or IPv6 network prefix in CIDR notation.

Signature

declare function fromString(input: string): Result<IpNetwork<IpAddress>, NetAddressError>

Parses a strict IPv4 network prefix in CIDR notation.

Signature

declare function ipv4FromString(input: string): Result<Ipv4Network, NetAddressError>

Parses a strict IPv6 network prefix in CIDR notation.

Signature

declare function ipv6FromString(input: string): Result<Ipv6Network, NetAddressError>

Encoding

format

Added in v4.0.0 Source

Formats a network prefix using canonical address text and its decimal prefix length.

Signature

declare function format(self: IpNetwork): string

Getters

addressCount

Added in v4.0.0 Source

Returns the exact number of addresses in a network prefix.

Signature

declare function addressCount(self: IpNetwork): bigint

firstAddress

Added in v4.0.0 Source

Returns the lowest address in a network prefix.

Signature

declare function firstAddress<A extends IpAddress>(self: IpNetwork<A>): A

lastAddress

Added in v4.0.0 Source

Returns the numerically greatest address in a network prefix.

Signature

declare function lastAddress<A extends IpAddress>(self: IpNetwork<A>): A

Guards

isIpNetwork

Added in v4.0.0 Source

Returns true when a value is an IPv4 or IPv6 network prefix.

Signature

declare function isIpNetwork(u: unknown): u is IpNetwork<IpAddress>

Returns true when a value is an IPv4 network prefix.

Signature

declare function isIpv4Network(u: unknown): u is Ipv4Network

Returns true when a value is an IPv6 network prefix.

Signature

declare function isIpv6Network(u: unknown): u is Ipv6Network

Models

IpNetwork interface

Added in v4.0.0 Source

An immutable canonical IP network prefix.

Signature

interface IpNetwork<out A extends NetAddress.IpAddress = NetAddress.IpAddress> extends Equal, Hash {
readonly _tag: "IpNetwork";
readonly "~effect/net/IpNetwork": "~effect/net/IpNetwork";
readonly address: A;
readonly prefixLength: number;
toJSON(): string;
toString(): string;
}

Ipv4Network type

Added in v4.0.0 Source

An immutable canonical IPv4 network prefix.

Signature

type Ipv4Network = IpNetwork<NetAddress.Ipv4Address>

Ipv6Network type

Added in v4.0.0 Source

An immutable canonical IPv6 network prefix.

Signature

type Ipv6Network = IpNetwork<NetAddress.Ipv6Address>

Predicates

contains

Added in v4.0.0 Source

Returns true when a network prefix contains an IP address.

Signature

declare const contains: {
(address: IpAddress): (self: IpNetwork) => boolean;
(self: IpNetwork, address: IpAddress): boolean;
}

Returns true when a network prefix contains every address in another network prefix.

Signature

declare const containsNetwork: {
(other: IpNetwork): (self: IpNetwork) => boolean;
(self: IpNetwork, other: IpNetwork): boolean;
}

overlaps

Added in v4.0.0 Source

Returns true when two network prefixes contain at least one common address.

Signature

declare const overlaps: {
(other: IpNetwork): (self: IpNetwork) => boolean;
(self: IpNetwork, other: IpNetwork): boolean;
}

Unsafe

Creates the network containing a trusted address, throwing when its prefix is invalid.

Signature

declare function fromAddressUnsafe<A extends IpAddress>(address: A, prefixLength: number): IpNetwork<A>

Parses a trusted network prefix in CIDR notation, throwing on failure.

Signature

declare function fromStringUnsafe(input: string): IpNetwork

makeUnsafe

Added in v4.0.0 Source

Creates a trusted network prefix, throwing when its address or prefix is invalid.

Signature

declare function makeUnsafe<A extends IpAddress>(address: A, prefixLength: number): IpNetwork<A>