From 3f5d03dc18e63773ce234041fb7a3031e98f951b Mon Sep 17 00:00:00 2001 From: Ib Green Date: Sun, 5 Apr 2026 10:15:00 -0400 Subject: [PATCH 1/3] feat(types) Add support for Float16Array --- .../types/api-reference/array-types.md | 6 +++++ modules/core/src/lib/gl-matrix.d.ts | 16 ++++++++++++++ modules/types/src/array-types.ts | 22 +++++++++++++++++-- modules/types/test/float16-types.ts | 21 ++++++++++++++++++ modules/types/test/is-array.spec.ts | 7 ++++++ 5 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 modules/types/test/float16-types.ts diff --git a/docs/modules/types/api-reference/array-types.md b/docs/modules/types/api-reference/array-types.md index 13e77732..db328e48 100644 --- a/docs/modules/types/api-reference/array-types.md +++ b/docs/modules/types/api-reference/array-types.md @@ -10,10 +10,14 @@ TypeScript types to simplify working with a mix of typed arrays and standard Jav Type matching any non-big JavaScript typed array. +When the environment's type libraries define `Float16Array`, it is included conditionally as well. + ### `TypedArrayConstructor` Type matching constructor for any non-big JavaScript typed array. +When the environment's type libraries define `Float16Array`, its constructor is included conditionally as well. + ### `BigTypedArray` Type matching any big JavaScript typed array. @@ -34,6 +38,8 @@ JavaScript number arrays of specific lengths. Type matching any classic JavaScript array containing numbers or any non-big typed array. +This conditionally includes `Float16Array` when the environment provides that type. + ### `NumericArray2-NumericArray16` Types matching number arrays of specific lengths or typed arrays. diff --git a/modules/core/src/lib/gl-matrix.d.ts b/modules/core/src/lib/gl-matrix.d.ts index 756c226c..ace10a4b 100644 --- a/modules/core/src/lib/gl-matrix.d.ts +++ b/modules/core/src/lib/gl-matrix.d.ts @@ -2,6 +2,20 @@ // SPDX-License-Identifier: MIT // Copyright (c) vis.gl contributors +/** + * Constructor type for `Float16Array` when the current TypeScript lib defines it. + * Resolves to `never` in environments where `Float16Array` is not available. + */ +type OptionalFloat16ArrayConstructor = + typeof globalThis extends {Float16Array: infer T} ? T : never; + +/** + * Instance type for `Float16Array` when the current TypeScript lib defines it. + * Resolves to `never` in environments where `Float16Array` is not available. + */ +type OptionalFloat16Array = + typeof globalThis extends {Float16Array: {prototype: infer T}} ? T : never; + type NumericArray = | Int8Array | Uint8Array @@ -12,6 +26,8 @@ type NumericArray = | Uint8ClampedArray | Float32Array | Float64Array + // Conditionally include Float16Array without hard-referencing the global symbol. + | OptionalFloat16Array | number[]; /* diff --git a/modules/types/src/array-types.ts b/modules/types/src/array-types.ts index a44bdd77..683921a2 100644 --- a/modules/types/src/array-types.ts +++ b/modules/types/src/array-types.ts @@ -2,6 +2,20 @@ // SPDX-License-Identifier: MIT // Copyright (c) vis.gl contributors +/** + * Constructor type for `Float16Array` when the current TypeScript lib defines it. + * Resolves to `never` in environments where `Float16Array` is not available. + */ +type OptionalFloat16ArrayConstructor = + typeof globalThis extends {Float16Array: infer T} ? T : never; + +/** + * Instance type for `Float16Array` when the current TypeScript lib defines it. + * Resolves to `never` in environments where `Float16Array` is not available. + */ +type OptionalFloat16Array = + typeof globalThis extends {Float16Array: {prototype: infer T}} ? T : never; + /** * Type covering all non-big typed arrays */ @@ -14,7 +28,9 @@ export type TypedArray = | Int32Array | Uint32Array | Float32Array - | Float64Array; + | Float64Array + // Conditionally include Float16Array without hard-referencing the global symbol. + | OptionalFloat16Array; /** * Type covering constructors for all non-big typed arrays @@ -28,7 +44,9 @@ export type TypedArrayConstructor = | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor - | Float64ArrayConstructor; + | Float64ArrayConstructor + // Conditionally include the Float16Array constructor without hard-referencing it. + | OptionalFloat16ArrayConstructor; /** * Type covering all big typed arrays diff --git a/modules/types/test/float16-types.ts b/modules/types/test/float16-types.ts new file mode 100644 index 00000000..d04f1b1f --- /dev/null +++ b/modules/types/test/float16-types.ts @@ -0,0 +1,21 @@ +// math.gl +// SPDX-License-Identifier: MIT +// Copyright (c) vis.gl contributors + +import type {NumericArray, TypedArray, TypedArrayConstructor} from '@math.gl/types'; + +type GlobalFloat16ArrayConstructor = + typeof globalThis extends {Float16Array: infer T} ? T : never; +type GlobalFloat16Array = + typeof globalThis extends {Float16Array: {prototype: infer T}} ? T : never; + +declare const float16Array: GlobalFloat16Array; +declare const float16ArrayConstructor: GlobalFloat16ArrayConstructor; + +const typedArray: TypedArray = float16Array; +const numericArray: NumericArray = float16Array; +const typedArrayConstructor: TypedArrayConstructor = float16ArrayConstructor; + +void typedArray; +void numericArray; +void typedArrayConstructor; diff --git a/modules/types/test/is-array.spec.ts b/modules/types/test/is-array.spec.ts index 4f4eebbf..f1fd9c5b 100644 --- a/modules/types/test/is-array.spec.ts +++ b/modules/types/test/is-array.spec.ts @@ -23,6 +23,13 @@ const TEST_CASES: {value: unknown; isTypedArray: boolean; isNumericArray: boolea {value: '', isTypedArray: false, isNumericArray: false} ]; +const Float16ArrayCtor = (globalThis as {Float16Array?: new (length: number) => ArrayBufferView}) + .Float16Array; + +if (Float16ArrayCtor) { + TEST_CASES.unshift({value: new Float16ArrayCtor(1), isTypedArray: true, isNumericArray: true}); +} + test('math.gl#isTypedArray', (t) => { for (const tc of TEST_CASES) { t.equal( From 65d2843a4dc6027d39db99b1328336d732dfabf2 Mon Sep 17 00:00:00 2001 From: Ib Green Date: Sun, 5 Apr 2026 10:33:26 -0400 Subject: [PATCH 2/3] fix --- modules/core/src/lib/gl-matrix.d.ts | 22 +++------- modules/geospatial/src/lng-lat-rectangle.ts | 2 +- modules/proj4/src/lib/proj4-projection.ts | 2 +- modules/types/src/array-types.ts | 47 ++++++++++----------- modules/web-mercator/src/fly-to-viewport.ts | 30 ++++++++----- 5 files changed, 51 insertions(+), 52 deletions(-) diff --git a/modules/core/src/lib/gl-matrix.d.ts b/modules/core/src/lib/gl-matrix.d.ts index ace10a4b..ba417e61 100644 --- a/modules/core/src/lib/gl-matrix.d.ts +++ b/modules/core/src/lib/gl-matrix.d.ts @@ -3,20 +3,9 @@ // Copyright (c) vis.gl contributors /** - * Constructor type for `Float16Array` when the current TypeScript lib defines it. - * Resolves to `never` in environments where `Float16Array` is not available. + * Base union for numeric arrays that are always available in the current lib set. */ -type OptionalFloat16ArrayConstructor = - typeof globalThis extends {Float16Array: infer T} ? T : never; - -/** - * Instance type for `Float16Array` when the current TypeScript lib defines it. - * Resolves to `never` in environments where `Float16Array` is not available. - */ -type OptionalFloat16Array = - typeof globalThis extends {Float16Array: {prototype: infer T}} ? T : never; - -type NumericArray = +type NumericArrayBase = | Int8Array | Uint8Array | Int16Array @@ -26,10 +15,13 @@ type NumericArray = | Uint8ClampedArray | Float32Array | Float64Array - // Conditionally include Float16Array without hard-referencing the global symbol. - | OptionalFloat16Array | number[]; +// Conditionally include Float16Array without hard-referencing the global symbol. +type NumericArray = typeof globalThis extends {Float16Array: {prototype: infer T}} + ? NumericArrayBase | T + : NumericArrayBase; + /* declare module 'gl-matrix/vec2' { function length(a: Readonly): number; diff --git a/modules/geospatial/src/lng-lat-rectangle.ts b/modules/geospatial/src/lng-lat-rectangle.ts index 8e4d4a06..238bfa32 100644 --- a/modules/geospatial/src/lng-lat-rectangle.ts +++ b/modules/geospatial/src/lng-lat-rectangle.ts @@ -55,7 +55,7 @@ export class LngLatRectangle { return result; } - get width() { + get width(): number { let east = this.east; const west = this.west; diff --git a/modules/proj4/src/lib/proj4-projection.ts b/modules/proj4/src/lib/proj4-projection.ts index a7b63ab9..8ee4b96f 100644 --- a/modules/proj4/src/lib/proj4-projection.ts +++ b/modules/proj4/src/lib/proj4-projection.ts @@ -7,7 +7,7 @@ import proj4 from 'proj4'; export class Proj4Projection { /** Define aliases for one or more projections */ static defineProjectionAliases(aliases: {[name: string]: string}): void { - const aliasArray = []; + const aliasArray: string[][] = []; for (const alias in aliases) { aliasArray.push([alias, aliases[alias]]); } diff --git a/modules/types/src/array-types.ts b/modules/types/src/array-types.ts index 683921a2..292b0973 100644 --- a/modules/types/src/array-types.ts +++ b/modules/types/src/array-types.ts @@ -3,23 +3,9 @@ // Copyright (c) vis.gl contributors /** - * Constructor type for `Float16Array` when the current TypeScript lib defines it. - * Resolves to `never` in environments where `Float16Array` is not available. + * Base union for non-big JavaScript typed arrays that are always available in the current lib set. */ -type OptionalFloat16ArrayConstructor = - typeof globalThis extends {Float16Array: infer T} ? T : never; - -/** - * Instance type for `Float16Array` when the current TypeScript lib defines it. - * Resolves to `never` in environments where `Float16Array` is not available. - */ -type OptionalFloat16Array = - typeof globalThis extends {Float16Array: {prototype: infer T}} ? T : never; - -/** - * Type covering all non-big typed arrays - */ -export type TypedArray = +type TypedArrayBase = | Int8Array | Uint8Array | Uint8ClampedArray @@ -28,14 +14,13 @@ export type TypedArray = | Int32Array | Uint32Array | Float32Array - | Float64Array - // Conditionally include Float16Array without hard-referencing the global symbol. - | OptionalFloat16Array; + | Float64Array; /** - * Type covering constructors for all non-big typed arrays + * Base union for constructors of non-big JavaScript typed arrays that are always available in the + * current lib set. */ -export type TypedArrayConstructor = +type TypedArrayConstructorBase = | Int8ArrayConstructor | Uint8ArrayConstructor | Uint8ClampedArrayConstructor @@ -44,9 +29,23 @@ export type TypedArrayConstructor = | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor - | Float64ArrayConstructor - // Conditionally include the Float16Array constructor without hard-referencing it. - | OptionalFloat16ArrayConstructor; + | Float64ArrayConstructor; + +/** + * Type covering all non-big typed arrays + */ +// Conditionally include Float16Array without hard-referencing the global symbol. +export type TypedArray = typeof globalThis extends {Float16Array: {prototype: infer T}} + ? TypedArrayBase | T + : TypedArrayBase; + +/** + * Type covering constructors for all non-big typed arrays + */ +// Conditionally include the Float16Array constructor without hard-referencing it. +export type TypedArrayConstructor = typeof globalThis extends {Float16Array: infer T} + ? TypedArrayConstructorBase | T + : TypedArrayConstructorBase; /** * Type covering all big typed arrays diff --git a/modules/web-mercator/src/fly-to-viewport.ts b/modules/web-mercator/src/fly-to-viewport.ts index 4971003c..fb555e5f 100644 --- a/modules/web-mercator/src/fly-to-viewport.ts +++ b/modules/web-mercator/src/fly-to-viewport.ts @@ -19,6 +19,12 @@ export type FlytoTransitionOptions = { maxDuration?: number; }; +type TransitionViewport = { + longitude: number; + latitude: number; + zoom: number; +}; + /** * mapbox-gl-js flyTo : https://www.mapbox.com/mapbox-gl-js/api/#map#flyto. * It implements “Smooth and efficient zooming and panning.” algorithm by @@ -44,14 +50,16 @@ export function flyToViewport( // If change in center is too small, do linear interpolaiton. if (u1 < EPSILON) { - const viewport = {}; + const viewport: TransitionViewport = { + longitude: 0, + latitude: 0, + zoom: 0 + }; for (const key of VIEWPORT_TRANSITION_PROPS) { const startValue = startProps[key]; const endValue = endProps[key]; - // @ts-ignore-error properties are populated dynamically viewport[key] = lerp(startValue, endValue, t); } - // @ts-expect-error properties are populated dynamically return viewport; } @@ -63,7 +71,7 @@ export function flyToViewport( const scaleIncrement = 1 / w; // Using w method for scaling. const newZoom = startZoom + scaleToZoom(scaleIncrement); - const newCenterWorld = vec2.scale([], uDelta, u); + const newCenterWorld = vec2.scale([] as number[], uDelta, u) as [number, number]; vec2.add(newCenterWorld, newCenterWorld, startCenterXY); const newCenter = worldToLngLat(newCenterWorld); @@ -103,8 +111,8 @@ function getFlyToTransitionParams( opts: FlytoTransitionOptions ): { startZoom: number; - startCenterXY: number[]; - uDelta: number[]; + startCenterXY: [number, number]; + uDelta: [number, number]; w0: number; u1: number; S: number; @@ -116,15 +124,15 @@ function getFlyToTransitionParams( opts = Object.assign({}, DEFAULT_OPTS, opts); const rho = opts.curve; const startZoom = startProps.zoom; - const startCenter = [startProps.longitude, startProps.latitude]; + const startCenter: [number, number] = [startProps.longitude, startProps.latitude]; const startScale = zoomToScale(startZoom); const endZoom = endProps.zoom; - const endCenter = [endProps.longitude, endProps.latitude]; + const endCenter: [number, number] = [endProps.longitude, endProps.latitude]; const scale = zoomToScale(endZoom - startZoom); - const startCenterXY = lngLatToWorld(startCenter); - const endCenterXY = lngLatToWorld(endCenter); - const uDelta = vec2.sub([] as number[], endCenterXY, startCenterXY); + const startCenterXY: [number, number] = lngLatToWorld(startCenter); + const endCenterXY: [number, number] = lngLatToWorld(endCenter); + const uDelta = vec2.sub([] as number[], endCenterXY, startCenterXY) as [number, number]; const w0 = Math.max(startProps.width, startProps.height); const w1 = w0 / scale; From 3c95a24175eb0bf5c3337675e840ecf73fe68213 Mon Sep 17 00:00:00 2001 From: Ib Green Date: Sun, 5 Apr 2026 10:35:22 -0400 Subject: [PATCH 3/3] prettier --- modules/types/test/float16-types.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/types/test/float16-types.ts b/modules/types/test/float16-types.ts index d04f1b1f..fe709361 100644 --- a/modules/types/test/float16-types.ts +++ b/modules/types/test/float16-types.ts @@ -4,10 +4,10 @@ import type {NumericArray, TypedArray, TypedArrayConstructor} from '@math.gl/types'; -type GlobalFloat16ArrayConstructor = - typeof globalThis extends {Float16Array: infer T} ? T : never; -type GlobalFloat16Array = - typeof globalThis extends {Float16Array: {prototype: infer T}} ? T : never; +type GlobalFloat16ArrayConstructor = typeof globalThis extends {Float16Array: infer T} ? T : never; +type GlobalFloat16Array = typeof globalThis extends {Float16Array: {prototype: infer T}} + ? T + : never; declare const float16Array: GlobalFloat16Array; declare const float16ArrayConstructor: GlobalFloat16ArrayConstructor;