Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tuple types for Coordinate, Size, Extent, etc. #15769

Open
janruo opened this issue Apr 24, 2024 · 1 comment
Open

Tuple types for Coordinate, Size, Extent, etc. #15769

janruo opened this issue Apr 24, 2024 · 1 comment

Comments

@janruo
Copy link
Contributor

janruo commented Apr 24, 2024

Currently the array types like Coordinate, Size, Extent are defined as Array<number>. This works for plain Javascript and for Typescript when the noUncheckedIndexedAccess compiler flag is disabled (default).

However, if extra type safety is wanted and the flag is enabled, those types become difficult to use. Typescript can't tell how many values a number array contains, so all extracted values will be number | undefined.

Would it be possible for Openlayers to define these types as tuples instead?

  • Extent would be [number, number, number, number]
  • Size would be [number, number]
  • Coordinate would probably have to be [number, number] | [number, number, number] | [number, number, number, number]

Example:

type CurrentCoordinate = number[];
type BetterCoordinate = [number, number] | [number, number, number] | [number, number, number, number];

const currentCoordinate = [1, 2] as CurrentCoordinate;
const betterCoordinate = [1, 2] as BetterCoordinate;
const betterXyzCoordinate = [1, 2, 3] as BetterCoordinate;

// Error: object is possibly 'undefined' :(
const foo = currentCoordinate[0] + 1;

// Just works :)
const bar = betterCoordinate[0] + 1;

// Any coordinate is guaranteed to have at least the X and Y
// x = number
// y = number
// z = number | undefined
// m = number | undefined
const [x, y, z, m] = betterXyzCoordinate;
@M393
Copy link
Contributor

M393 commented Apr 24, 2024

Here is a previous discussion: #14166

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants