Type Alias Position

Position: number[]

A Position is an array of coordinates. https://tools.ietf.org/html/rfc7946#section-3.1.1 Array should contain between two and three elements. The previous GeoJSON specification allowed more elements (e.g., which could be used to represent M values), but the current specification only allows X, Y, and (optionally) Z to be defined.

Note: the type will not be narrowed down to [number, number] | [number, number, number] due to marginal benefits and the large impact of breaking change.

See previous discussions on the type narrowing:

One can use a type guard that returns a type predicate to determine if a position is a 2D or 3D position.

import type { Position } from 'geojson';

type StrictPosition = [x: number, y: number] | [x: number, y: number, z: number]

function isStrictPosition(position: Position): position is StrictPosition {
return position.length === 2 || position.length === 3
};

let position: Position = [-116.91, 45.54];

let x: number;
let y: number;
let z: number | undefined;

if (isStrictPosition(position)) {
// `tsc` would throw an error if we tried to destructure a fourth parameter
[x, y, z] = position;
} else {
throw new TypeError("Position is not a 2D or 3D point");
}