Skip to content

Commit 1abdb88

Browse files
improve types
1 parent 50d9756 commit 1abdb88

5 files changed

Lines changed: 53 additions & 49 deletions

File tree

src/geo/projection/globe_projection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {type CanonicalTileID} from '../../tile/tile_id';
1212
import {type Mesh} from '../../render/mesh';
1313

1414
type ProjectionProps = {
15-
type: DataConstantProperty<ProjectionDefinition>;
15+
type: DataConstantProperty<ProjectionDefinitionSpecification>;
1616
};
1717

1818
type ProjectionPossiblyEvaluated = {

src/style/style.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ import type {
5656
DiffOperations,
5757
ProjectionSpecification,
5858
SkySpecification,
59-
StateSpecification
59+
StateSpecification,
60+
AllPaintProperties,
61+
AllLayoutProperties,
6062
} from '@maplibre/maplibre-gl-style-spec';
6163
import type {CanvasSourceSpecification} from '../source/canvas_source';
6264
import type {CustomLayerInterface} from './style_layer/custom_style_layer';
@@ -1315,7 +1317,7 @@ export class Style extends Evented {
13151317
return clone(this.getLayer(layer).filter);
13161318
}
13171319

1318-
setLayoutProperty(layerId: string, name: string, value: any, options: StyleSetterOptions = {}) {
1320+
setLayoutProperty<K extends keyof AllLayoutProperties>(layerId: string, name: K, value: AllLayoutProperties[K], options: StyleSetterOptions = {}) {
13191321
this._checkLoaded();
13201322

13211323
const layer = this.getLayer(layerId);
@@ -1336,7 +1338,7 @@ export class Style extends Evented {
13361338
* @param name - the name of the layout property
13371339
* @returns the property value
13381340
*/
1339-
getLayoutProperty(layerId: string, name: string) {
1341+
getLayoutProperty<K extends keyof AllLayoutProperties>(layerId: string, name: K): AllLayoutProperties[K] | undefined {
13401342
const layer = this.getLayer(layerId);
13411343
if (!layer) {
13421344
this.fire(new ErrorEvent(new Error(`Cannot get style of non-existing layer "${layerId}".`)));
@@ -1346,7 +1348,7 @@ export class Style extends Evented {
13461348
return layer.getLayoutProperty(name);
13471349
}
13481350

1349-
setPaintProperty(layerId: string, name: string, value: any, options: StyleSetterOptions = {}) {
1351+
setPaintProperty<K extends keyof AllPaintProperties>(layerId: string, name: K, value: AllPaintProperties[K], options: StyleSetterOptions = {}) {
13501352
this._checkLoaded();
13511353

13521354
const layer = this.getLayer(layerId);
@@ -1360,14 +1362,14 @@ export class Style extends Evented {
13601362
this._updatePaintProperty(layer, name, value, options);
13611363
}
13621364

1363-
_updatePaintProperty(layer: StyleLayer, name: string, value: any, options: StyleSetterOptions = {}) {
1365+
_updatePaintProperty<K extends keyof AllPaintProperties>(layer: StyleLayer, name: K, value: AllPaintProperties[K], options: StyleSetterOptions = {}) {
13641366
const requiresRelayout = layer.setPaintProperty(name, value, options);
13651367
if (requiresRelayout) {
13661368
this._updateLayer(layer);
13671369
}
13681370

13691371
if (isRasterStyleLayer(layer) && name === 'raster-fade-duration') {
1370-
this.tileManagers[layer.source].setRasterFadeDuration(value);
1372+
this.tileManagers[layer.source].setRasterFadeDuration(value as number);
13711373
}
13721374

13731375
this._changed = true;
@@ -1376,7 +1378,7 @@ export class Style extends Evented {
13761378
this._serializedLayers = null;
13771379
}
13781380

1379-
getPaintProperty(layer: string, name: string) {
1381+
getPaintProperty<K extends keyof AllPaintProperties>(layer: string, name: K): AllPaintProperties[K] {
13801382
return this.getLayer(layer).getPaintProperty(name);
13811383
}
13821384

src/style/style_layer.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import type {
1818
LayerSpecification,
1919
FilterSpecification,
2020
VisibilitySpecification,
21-
VisibilityExpression
21+
VisibilityExpression,
22+
AllPaintProperties,
23+
AllLayoutProperties,
2224
} from '@maplibre/maplibre-gl-style-spec';
2325
import type {TransitionParameters, PropertyValue} from './properties';
2426
import {type EvaluationParameters} from './evaluation_parameters';
@@ -32,6 +34,8 @@ import {type mat4} from 'gl-matrix';
3234
import type {UnwrappedTileID} from '../tile/tile_id';
3335
import type {VectorTileFeatureLike} from '@maplibre/vt-pbf';
3436

37+
type PaintPropertyEntry = { [K in keyof AllPaintProperties]: {name: K; value: AllPaintProperties[K]} }[keyof AllPaintProperties];
38+
3539
export type QueryIntersectsFeatureParams = {
3640
/**
3741
* The geometry to check intersection with.
@@ -153,10 +157,10 @@ export abstract class StyleLayer extends Evented {
153157
this._transitionablePaint = new Transitionable(properties.paint, globalState);
154158

155159
for (const property in layer.paint) {
156-
this.setPaintProperty(property, layer.paint[property], {validate: false});
160+
this.setPaintProperty(property as keyof AllPaintProperties, layer.paint[property as keyof typeof layer.paint], {validate: false});
157161
}
158162
for (const property in layer.layout) {
159-
this.setLayoutProperty(property, layer.layout[property], {validate: false});
163+
this.setLayoutProperty(property as keyof AllLayoutProperties, layer.layout[property as keyof typeof layer.layout], {validate: false});
160164
}
161165

162166
this._transitioningPaint = this._transitionablePaint.untransitioned();
@@ -174,9 +178,9 @@ export abstract class StyleLayer extends Evented {
174178
return this._crossfadeParameters;
175179
}
176180

177-
getLayoutProperty(name: string) {
181+
getLayoutProperty<K extends keyof AllLayoutProperties>(name: K): AllLayoutProperties[K] {
178182
if (name === 'visibility') {
179-
return this.visibility;
183+
return this.visibility as AllLayoutProperties[K];
180184
}
181185
if (this._transitionablePaint?.hasProperty(name)) {
182186
throw new Error(name + ERROR_PAINT_NOT_LAYOUT);
@@ -221,16 +225,16 @@ export abstract class StyleLayer extends Evented {
221225
* This is used to determine if layer needs to be repainted when global state property changes.
222226
*
223227
*/
224-
getPaintAffectingGlobalStateRefs(): globalThis.Map<string, Array<{name: string; value: any}>> {
225-
const globalStateRefs = new globalThis.Map<string, Array<{name: string; value: any}>>();
228+
getPaintAffectingGlobalStateRefs(): globalThis.Map<string, Array<PaintPropertyEntry>> {
229+
const globalStateRefs = new globalThis.Map<string, Array<PaintPropertyEntry>>();
226230

227231
if (this._transitionablePaint) {
228232
for (const propertyName in this._transitionablePaint._values) {
229233
const value = this._transitionablePaint._values[propertyName].value;
230234

231235
for (const globalStateRef of value.getGlobalStateRefs()) {
232236
const properties = globalStateRefs.get(globalStateRef) ?? [];
233-
properties.push({name: propertyName, value: value.value});
237+
properties.push({name: propertyName as keyof AllPaintProperties, value: value.value} as PaintPropertyEntry);
234238
globalStateRefs.set(globalStateRef, properties);
235239
}
236240
}
@@ -247,10 +251,10 @@ export abstract class StyleLayer extends Evented {
247251
return this._visibilityExpression.getGlobalStateRefs();
248252
}
249253

250-
setLayoutProperty(name: string, value: any, options: StyleSetterOptions = {}) {
254+
setLayoutProperty<K extends keyof AllLayoutProperties>(name: K, value: AllLayoutProperties[K], options: StyleSetterOptions = {}) {
251255
if (name === 'visibility') {
252-
this.visibility = value;
253-
this._visibilityExpression.setValue(value);
256+
this.visibility = value as VisibilitySpecification;
257+
this._visibilityExpression.setValue(value as VisibilitySpecification);
254258
this.recalculateVisibility();
255259
return;
256260
}
@@ -265,23 +269,23 @@ export abstract class StyleLayer extends Evented {
265269
this._unevaluatedLayout.setValue(name, value);
266270
}
267271

268-
getPaintProperty(name: string) {
272+
getPaintProperty<K extends keyof AllPaintProperties>(name: K): AllPaintProperties[K] {
269273
if (name.endsWith(TRANSITION_SUFFIX)) {
270274
const baseName = name.slice(0, -TRANSITION_SUFFIX.length);
271275
if (baseName === 'visibility' || this._unevaluatedLayout?.hasProperty(baseName)) {
272276
throw new Error(name + ERROR_LAYOUT_NOT_PAINT);
273277
}
274-
return this._transitionablePaint.getTransition(baseName);
278+
return this._transitionablePaint.getTransition(baseName) as AllPaintProperties[K];
275279
} else {
276-
if (name === 'visibility' || this._unevaluatedLayout?.hasProperty(name)) {
280+
if (this._unevaluatedLayout?.hasProperty(name)) {
277281
throw new Error(name + ERROR_LAYOUT_NOT_PAINT);
278282
}
279-
return this._transitionablePaint.getValue(name);
283+
return this._transitionablePaint.getValue(name) as AllPaintProperties[K];
280284
}
281285
}
282286

283-
setPaintProperty(name: string, value: unknown, options: StyleSetterOptions = {}) {
284-
if (name === 'visibility' || (this._unevaluatedLayout?.hasProperty(name))) {
287+
setPaintProperty<K extends keyof AllPaintProperties>(name: K, value: AllPaintProperties[K], options: StyleSetterOptions = {}) {
288+
if (this._unevaluatedLayout?.hasProperty(name)) {
285289
this.fire(new ErrorEvent(new Error(name + ERROR_LAYOUT_NOT_PAINT)));
286290
return false;
287291
}

src/ui/map.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ import type {
6060
TerrainSpecification,
6161
ProjectionSpecification,
6262
SkySpecification,
63+
AllPaintProperties,
64+
AllLayoutProperties,
6365
} from '@maplibre/maplibre-gl-style-spec';
6466
import type {CanvasSourceSpecification} from '../source/canvas_source';
6567
import type {GeoJSONFeature, MapGeoJSONFeature} from '../util/vectortile_to_geojson';
@@ -2992,7 +2994,7 @@ export class Map extends Camera {
29922994
* @see [Change a layer's color with buttons](https://maplibre.org/maplibre-gl-js/docs/examples/change-a-layers-color-with-buttons/)
29932995
* @see [Create a draggable point](https://maplibre.org/maplibre-gl-js/docs/examples/create-a-draggable-point/)
29942996
*/
2995-
setPaintProperty(layerId: string, name: string, value: any, options: StyleSetterOptions = {}): this {
2997+
setPaintProperty<K extends keyof AllPaintProperties>(layerId: string, name: K, value: AllPaintProperties[K], options: StyleSetterOptions = {}): this {
29962998
this.style.setPaintProperty(layerId, name, value, options);
29972999
return this._update(true);
29983000
}
@@ -3004,7 +3006,7 @@ export class Map extends Camera {
30043006
* @param name - The name of a paint property to get.
30053007
* @returns The value of the specified paint property.
30063008
*/
3007-
getPaintProperty(layerId: string, name: string) {
3009+
getPaintProperty<K extends keyof AllPaintProperties>(layerId: string, name: K): AllPaintProperties[K] {
30083010
return this.style.getPaintProperty(layerId, name);
30093011
}
30103012

@@ -3020,7 +3022,7 @@ export class Map extends Camera {
30203022
* map.setLayoutProperty('my-layer', 'visibility', 'none');
30213023
* ```
30223024
*/
3023-
setLayoutProperty(layerId: string, name: string, value: any, options: StyleSetterOptions = {}): this {
3025+
setLayoutProperty<K extends keyof AllLayoutProperties>(layerId: string, name: K, value: AllLayoutProperties[K], options: StyleSetterOptions = {}): this {
30243026
this.style.setLayoutProperty(layerId, name, value, options);
30253027
return this._update(true);
30263028
}
@@ -3032,7 +3034,7 @@ export class Map extends Camera {
30323034
* @param name - The name of the layout property to get.
30333035
* @returns The value of the specified layout property.
30343036
*/
3035-
getLayoutProperty(layerId: string, name: string) {
3037+
getLayoutProperty<K extends keyof AllLayoutProperties>(layerId: string, name: K): AllLayoutProperties[K] | undefined {
30363038
return this.style.getLayoutProperty(layerId, name);
30373039
}
30383040

test/bench/benchmarks/terrain_render.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,30 @@ export default class TerrainRender extends Benchmark {
1414
height: 768,
1515
center: [10.5, 46.9],
1616
pitch: 60,
17-
style: {
18-
version: 8,
19-
sources: {
20-
'terrain-rgb': {
21-
'type': 'raster-dem',
22-
'url': 'https://tiles.mapterhorn.com/tilejson.json'
23-
}
24-
},
25-
terrain: {
26-
source: 'terrain-rgb',
27-
exaggeration: 1
28-
},
29-
layers: [
30-
{
31-
'id': 'background',
32-
'type': 'background',
33-
'paint': {'background-color': '#f8f4f0'}
34-
}
35-
]
36-
},
17+
style: 'https://tiles.openfreemap.org/styles/liberty',
3718
idle: true
3819
});
20+
21+
this.map.addSource('terrain-dem', {
22+
type: 'raster-dem',
23+
url: 'https://tiles.mapterhorn.com/tilejson.json'
24+
});
25+
this.map.setTerrain({source: 'terrain-dem', exaggeration: 1.5});
26+
27+
// Wait for DEM tiles to load
28+
await this.map.once('idle');
3929
} catch (error) {
4030
console.error(error);
4131
}
4232
}
4333

34+
_bearing: number = 0;
35+
4436
bench() {
37+
// Rotate the camera slightly each frame to force depth pre-pass to re-run
38+
// and symbol layers to recalculate visibility against terrain depth
39+
this._bearing = (this._bearing + 0.5) % 360;
40+
this.map.setBearing(this._bearing);
4541
Benchmark.renderMap(this.map);
4642
}
4743

0 commit comments

Comments
 (0)