Skip to content

Commit d3a9e78

Browse files
stepankuzmingithub-actions[bot]
authored andcommitted
Expose TypeScript types from @mapbox/mapbox-gl-style-spec (internal-8754)
GitOrigin-RevId: 6fcbb016c646b72891ba7a9ff2d7d5912b97cd2f
1 parent 4004f0b commit d3a9e78

File tree

3 files changed

+184
-15
lines changed

3 files changed

+184
-15
lines changed

src/style-spec/style-spec.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {eachSource, eachLayer, eachProperty} from './visit';
1717
import validate from './validate_style';
1818
import validateMapboxApiSupported from './validate_mapbox_api_supported';
1919

20+
export type * from './types';
21+
2022
type ExpressionType = 'data-driven' | 'color-ramp' | 'data-constant' | 'constant';
2123

2224
type ExpressionParameter =
@@ -32,7 +34,7 @@ type ExpressionParameter =
3234
| 'distance-from-center'
3335
| 'raster-particle-speed';
3436

35-
export type ExpressionSpecification = {
37+
export type PropertyExpressionSpecification = {
3638
interpolated: boolean,
3739
parameters?: ExpressionParameter[],
3840
relaxZoomRestriction?: boolean
@@ -43,7 +45,7 @@ export type ArrayPropertySpecification =
4345
type: 'array';
4446
'property-type': ExpressionType;
4547
value: 'enum';
46-
expression?: ExpressionSpecification,
48+
expression?: PropertyExpressionSpecification,
4749
transition?: boolean,
4850
default?: string[],
4951
length?: number,
@@ -59,7 +61,7 @@ export type ArrayPropertySpecification =
5961
type: 'array';
6062
'property-type': ExpressionType;
6163
value: 'number';
62-
expression?: ExpressionSpecification;
64+
expression?: PropertyExpressionSpecification;
6365
transition?: boolean;
6466
default?: number[];
6567
minimum?: number;
@@ -77,7 +79,7 @@ export type ArrayPropertySpecification =
7779
type: 'array';
7880
'property-type': ExpressionType;
7981
value: 'string';
80-
expression?: ExpressionSpecification;
82+
expression?: PropertyExpressionSpecification;
8183
transition?: boolean;
8284
default?: string[];
8385
length?: number;
@@ -94,7 +96,7 @@ export type ArrayPropertySpecification =
9496
export type BooleanPropertySpecification = {
9597
type: 'boolean';
9698
'property-type': ExpressionType;
97-
expression?: ExpressionSpecification;
99+
expression?: PropertyExpressionSpecification;
98100
transition?: boolean;
99101
default?: boolean;
100102
overridable?: boolean;
@@ -108,7 +110,7 @@ export type BooleanPropertySpecification = {
108110
export type ColorPropertySpecification = {
109111
type: 'color';
110112
'property-type': ExpressionType;
111-
expression?: ExpressionSpecification;
113+
expression?: PropertyExpressionSpecification;
112114
transition?: boolean;
113115
default?: string;
114116
'use-theme'?: boolean;
@@ -123,7 +125,7 @@ export type ColorPropertySpecification = {
123125
export type EnumPropertySpecification = {
124126
type: 'enum';
125127
'property-type': ExpressionType;
126-
expression?: ExpressionSpecification;
128+
expression?: PropertyExpressionSpecification;
127129
transition?: boolean;
128130
default?: string;
129131
values?: {[_: string]: unknown};
@@ -137,7 +139,7 @@ export type EnumPropertySpecification = {
137139
export type FormattedPropertySpecification = {
138140
type: 'formatted';
139141
'property-type': ExpressionType;
140-
expression?: ExpressionSpecification;
142+
expression?: PropertyExpressionSpecification;
141143
transition?: boolean;
142144
default?: string;
143145
tokens?: boolean;
@@ -150,7 +152,7 @@ export type FormattedPropertySpecification = {
150152
export type NumberPropertySpecification = {
151153
type: 'number';
152154
'property-type': ExpressionType;
153-
expression?: ExpressionSpecification;
155+
expression?: PropertyExpressionSpecification;
154156
transition?: boolean;
155157
default?: number;
156158
minimum?: number;
@@ -167,7 +169,7 @@ export type NumberPropertySpecification = {
167169
export type ResolvedImagePropertySpecification = {
168170
type: 'resolvedImage';
169171
'property-type': ExpressionType;
170-
expression?: ExpressionSpecification;
172+
expression?: PropertyExpressionSpecification;
171173
transition?: boolean;
172174
default?: string;
173175
tokens?: boolean;
@@ -181,7 +183,7 @@ export type ResolvedImagePropertySpecification = {
181183
export type StringPropertySpecification = {
182184
type: 'string';
183185
'property-type': ExpressionType;
184-
expression?: ExpressionSpecification;
186+
expression?: PropertyExpressionSpecification;
185187
transition?: boolean;
186188
default?: string;
187189
tokens?: boolean;

src/style-spec/util/properties.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type {ExpressionSpecification, StylePropertySpecification} from '../style-spec';
1+
import type {PropertyExpressionSpecification, StylePropertySpecification} from '../style-spec';
22

3-
type ExpressionParameter = ExpressionSpecification['parameters'][number];
3+
type ExpressionParameter = PropertyExpressionSpecification['parameters'][number];
44

55
function expressionHasParameter(
6-
expression: ExpressionSpecification | null | undefined,
6+
expression: PropertyExpressionSpecification | null | undefined,
77
parameter: ExpressionParameter,
88
): boolean {
99
return !!expression && !!expression.parameters && expression.parameters.indexOf(parameter) > -1;
Lines changed: 168 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,168 @@
1-
import * as styleSpec from '@mapbox/mapbox-gl-style-spec/style-spec.ts';
1+
/* eslint-disable */
2+
3+
import type {
4+
StyleSpecification,
5+
SourceSpecification,
6+
LayerSpecification,
7+
FilterSpecification,
8+
ExpressionSpecification,
9+
TransitionSpecification,
10+
VectorSourceSpecification,
11+
RasterSourceSpecification,
12+
GeoJSONSourceSpecification,
13+
FillLayerSpecification,
14+
LineLayerSpecification,
15+
SymbolLayerSpecification,
16+
CircleLayerSpecification,
17+
BackgroundLayerSpecification,
18+
DataDrivenPropertyValueSpecification,
19+
PropertyValueSpecification,
20+
} from '@mapbox/mapbox-gl-style-spec';
21+
22+
//
23+
// StyleSpecification
24+
//
25+
26+
const style: StyleSpecification = {
27+
version: 8,
28+
sources: {
29+
'vector-source': {
30+
type: 'vector',
31+
url: 'mapbox://mapbox.mapbox-streets-v8'
32+
}
33+
},
34+
layers: [
35+
{
36+
id: 'background',
37+
type: 'background',
38+
paint: {
39+
'background-color': '#000'
40+
}
41+
}
42+
]
43+
};
44+
45+
style.version satisfies 8;
46+
style.sources satisfies Record<string, SourceSpecification> | undefined;
47+
style.layers satisfies LayerSpecification[];
48+
49+
//
50+
// SourceSpecification
51+
//
52+
53+
const vectorSource: VectorSourceSpecification = {
54+
type: 'vector',
55+
url: 'mapbox://mapbox.mapbox-streets-v8'
56+
};
57+
vectorSource satisfies SourceSpecification;
58+
59+
const rasterSource: RasterSourceSpecification = {
60+
type: 'raster',
61+
url: 'mapbox://mapbox.satellite',
62+
tileSize: 256
63+
};
64+
rasterSource satisfies SourceSpecification;
65+
66+
const geojsonSource: GeoJSONSourceSpecification = {
67+
type: 'geojson',
68+
data: {
69+
type: 'FeatureCollection',
70+
features: []
71+
}
72+
};
73+
geojsonSource satisfies SourceSpecification;
74+
75+
//
76+
// LayerSpecification
77+
//
78+
79+
const fillLayer: FillLayerSpecification = {
80+
id: 'fill-layer',
81+
type: 'fill',
82+
source: 'vector-source',
83+
paint: {
84+
'fill-color': '#ff0000',
85+
'fill-opacity': 0.5
86+
}
87+
};
88+
fillLayer satisfies LayerSpecification;
89+
90+
const lineLayer: LineLayerSpecification = {
91+
id: 'line-layer',
92+
type: 'line',
93+
source: 'vector-source',
94+
paint: {
95+
'line-color': '#0000ff',
96+
'line-width': 2
97+
}
98+
};
99+
lineLayer satisfies LayerSpecification;
100+
101+
const symbolLayer: SymbolLayerSpecification = {
102+
id: 'symbol-layer',
103+
type: 'symbol',
104+
source: 'vector-source',
105+
layout: {
106+
'text-field': ['get', 'name'],
107+
'text-size': 12
108+
}
109+
};
110+
symbolLayer satisfies LayerSpecification;
111+
112+
const circleLayer: CircleLayerSpecification = {
113+
id: 'circle-layer',
114+
type: 'circle',
115+
source: 'vector-source',
116+
paint: {
117+
'circle-radius': 5,
118+
'circle-color': '#00ff00'
119+
}
120+
};
121+
circleLayer satisfies LayerSpecification;
122+
123+
const backgroundLayer: BackgroundLayerSpecification = {
124+
id: 'background',
125+
type: 'background',
126+
paint: {
127+
'background-color': '#ffffff'
128+
}
129+
};
130+
backgroundLayer satisfies LayerSpecification;
131+
132+
//
133+
// FilterSpecification
134+
//
135+
136+
const legacyFilter: FilterSpecification = ['==', 'type', 'residential'];
137+
const expressionFilter: FilterSpecification = ['all', ['==', ['get', 'type'], 'residential'], ['>', ['get', 'area'], 1000]];
138+
139+
//
140+
// ExpressionSpecification
141+
//
142+
143+
const getExpr: ExpressionSpecification = ['get', 'name'];
144+
const interpolateExpr: ExpressionSpecification = ['interpolate', ['linear'], ['zoom'], 10, 1, 15, 5];
145+
const caseExpr: ExpressionSpecification = ['case', ['has', 'name'], ['get', 'name'], 'unnamed'];
146+
147+
//
148+
// TransitionSpecification
149+
//
150+
151+
const transition: TransitionSpecification = {
152+
duration: 300,
153+
delay: 0
154+
};
155+
156+
//
157+
// DataDrivenPropertyValueSpecification
158+
//
159+
160+
const dataDrivenColor: DataDrivenPropertyValueSpecification<string> = ['get', 'color'];
161+
const staticColor: DataDrivenPropertyValueSpecification<string> = '#ff0000';
162+
163+
//
164+
// PropertyValueSpecification
165+
//
166+
167+
const zoomBasedOpacity: PropertyValueSpecification<number> = ['interpolate', ['linear'], ['zoom'], 10, 0, 15, 1];
168+
const staticOpacity: PropertyValueSpecification<number> = 0.8;

0 commit comments

Comments
 (0)