Skip to content

Commit

Permalink
Minor typing fixes (internal-1833)
Browse files Browse the repository at this point in the history
* fix Map#_controlPositions

* Expose `RequestTransformFunction`
  • Loading branch information
stepankuzmin authored and underoot committed Sep 12, 2024
1 parent 00dd47d commit 089d5fe
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 15 deletions.
13 changes: 6 additions & 7 deletions src/geo/projection/adjustments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import {clamp, smoothstep} from '../../util/util';
import type Projection from './projection';
import type Transform from '../transform';

export default function getProjectionAdjustments(transform: Transform, withoutRotation?: boolean): Array<number> {
export default function getProjectionAdjustments(transform: Transform, withoutRotation?: boolean): number[] {
const interpT = getProjectionInterpolationT(transform.projection, transform.zoom, transform.width, transform.height);
const matrix = getShearAdjustment(transform.projection, transform.zoom, transform.center, interpT, withoutRotation);

const scaleAdjustment = getScaleAdjustment(transform);
mat4.scale(matrix, matrix, [scaleAdjustment, scaleAdjustment, 1]);

// @ts-expect-error - TS2322 - Type 'mat4' is not assignable to type 'number[]'.
return matrix;
return matrix as number[];
}

export function getScaleAdjustment(transform: Transform): number {
Expand All @@ -26,12 +25,12 @@ export function getScaleAdjustment(transform: Transform): number {
return scaleAdjustment;
}

export function getProjectionAdjustmentInverted(transform: Transform): Array<number> {
export function getProjectionAdjustmentInverted(transform: Transform): number[] {
const m = getProjectionAdjustments(transform, true);
// @ts-expect-error - TS2322 - Type 'mat2' is not assignable to type 'number[]'.
return mat2.invert([] as any, [
return mat2.invert([] as unknown as mat2, [
m[0], m[1],
m[4], m[5]]);
m[4], m[5]]
) as number[];
}

export function getProjectionInterpolationT(
Expand Down
3 changes: 1 addition & 2 deletions src/geo/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2309,8 +2309,7 @@ class Transform {
const mc = this.locationCoordinate(this.center);
const adjustments = mat4.identity([] as any);
mat4.translate(adjustments, adjustments, [mc.x * this.worldSize, mc.y * this.worldSize, 0]);
// @ts-expect-error - TS2345 - Argument of type 'number[]' is not assignable to parameter of type 'ReadonlyMat4'.
mat4.multiply(adjustments, adjustments, getProjectionAdjustments(this));
mat4.multiply(adjustments, adjustments, getProjectionAdjustments(this) as mat4);
mat4.translate(adjustments, adjustments, [-mc.x * this.worldSize, -mc.y * this.worldSize, 0]);
mat4.multiply(m, m, adjustments);
// @ts-expect-error - TS2345 - Argument of type 'number[] | Float32Array' is not assignable to parameter of type 'mat4'.
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export type {PluginStatus} from './source/rtl_text_plugin';
export type {Event, ErrorEvent} from './util/evented';
export type {GeoJSONFeature} from './util/vectortile_to_geojson';
export type {PaddingOptions} from './geo/edge_insets';
export type {RequestParameters} from './util/ajax';
export type {RequestTransformFunction, ResourceType} from './util/mapbox';
export type {LngLatLike, LngLatBoundsLike} from './geo/lng_lat';

export type {FeatureSelector} from './style/style';
Expand Down
2 changes: 1 addition & 1 deletion src/ui/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ export class Map extends Camera {
_canvasContainer: HTMLElement;
_controlContainer: HTMLElement;
_controlPositions: {
[_: string]: HTMLElement;
[p in ControlPosition]?: HTMLElement;
};
_interactive?: boolean;
_showTileBoundaries?: boolean;
Expand Down
8 changes: 4 additions & 4 deletions src/util/mapbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import {postData, getData} from './ajax';
import {getLivePerformanceMetrics} from '../util/live_performance';

import type {LivePerformanceData} from '../util/live_performance';
import type {RequestParameters, ResourceType} from './ajax';
import type {RequestParameters, ResourceType as ResourceTypeEnum} from './ajax';
import type {Cancelable} from '../types/cancelable';
import type {TileJSON} from '../types/tilejson';
import type {Map as MapboxMap} from "../ui/map";

type ResourceTypeEnum = keyof typeof ResourceType;
export type RequestTransformFunction = (url: string, resourceType?: ResourceTypeEnum) => RequestParameters;
export type ResourceType = keyof typeof ResourceTypeEnum;
export type RequestTransformFunction = (url: string, resourceTypeEnum?: ResourceType) => RequestParameters;

type UrlObject = {
protocol: string;
Expand Down Expand Up @@ -65,7 +65,7 @@ export class RequestManager {
return Date.now() > this._skuTokenExpiresAt;
}

transformRequest(url: string, type: ResourceTypeEnum): RequestParameters {
transformRequest(url: string, type: ResourceType): RequestParameters {
if (this._transformRequestFn) {
return this._transformRequestFn(url, type) || {url};
}
Expand Down
2 changes: 1 addition & 1 deletion test/build/typings/compatibility-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ mercatorcoordinate.meterInMercatorCoordinateUnits() satisfies number;

expectType<mapboxgl.TransformRequestFunction>((url: string) => ({url}));
// @ts-expect-error - incompatible
expectType<mapboxgl.TransformRequestFunction>((url: string, resourceType: mapboxgl.ResourceType) => ({
expectType<mapboxgl.TransformRequestFunction>((url: string, resourceTypeEnum: mapboxgl.ResourceTypeEnum) => ({
url,
credentials: "same-origin",
headers: {"Accept-Encoding": "compress"},
Expand Down
2 changes: 2 additions & 0 deletions test/build/typings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const map = new mapboxgl.Map({
attributionControl: false,
});

const transformRequest: mapboxgl.RequestTransformFunction = (url: string, resourceTypeEnum: mapboxgl.ResourceType): mapboxgl.RequestParameters => {return {url}};

//
// Events
//
Expand Down

0 comments on commit 089d5fe

Please sign in to comment.