From 4ebcc8fea949de6522a70650aae4a0e50df9e4e6 Mon Sep 17 00:00:00 2001 From: Dmytro Gokun Date: Tue, 22 Oct 2019 23:15:13 +0300 Subject: [PATCH] [mapbox-gl] add Visibility, Layout & AnyLayout types to facilitate further type-safety (#39264) --- types/mapbox-gl/index.d.ts | 43 ++++++++++++++---------------- types/mapbox-gl/mapbox-gl-tests.ts | 13 +++++++++ 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/types/mapbox-gl/index.d.ts b/types/mapbox-gl/index.d.ts index 624b3ee767..089a293045 100644 --- a/types/mapbox-gl/index.d.ts +++ b/types/mapbox-gl/index.d.ts @@ -251,11 +251,11 @@ declare namespace mapboxgl { export interface MapboxOptions { /** - * If true, the gl context will be created with MSA antialiasing, which can be useful for antialiasing custom layers. + * If true, the gl context will be created with MSA antialiasing, which can be useful for antialiasing custom layers. * This is false by default as a performance optimization. */ antialias?: boolean; - + /** If true, an attribution control will be added to the map. */ attributionControl?: boolean; @@ -1369,6 +1369,8 @@ declare namespace mapboxgl { touchcancel: MapLayerTouchEvent; } + export type AnyLayout = BackgroundLayout | FillLayout | FillExtrusionLayout | LineLayout | SymbolLayout | RasterLayout | CircleLayout | HeatmapLayout | HillshadeLayout; + export interface Layer { id: string; type?: 'fill' | 'line' | 'symbol' | 'circle' | 'fill-extrusion' | 'raster' | 'background' | 'heatmap' | 'hillshade'; @@ -1386,7 +1388,7 @@ declare namespace mapboxgl { interactive?: boolean; filter?: any[]; - layout?: BackgroundLayout | FillLayout | FillExtrusionLayout | LineLayout | SymbolLayout | RasterLayout | CircleLayout | HeatmapLayout | HillshadeLayout; + layout?: AnyLayout; paint?: BackgroundPaint | FillPaint | FillExtrusionPaint | LinePaint | SymbolPaint | RasterPaint | CirclePaint | HeatmapPaint | HillshadePaint; } @@ -1470,8 +1472,13 @@ declare namespace mapboxgl { 'colorSpace'?: 'rgb' | 'lab' | 'hcl'; } - export interface BackgroundLayout { - visibility?: 'visible' | 'none'; + type Visibility = 'visible' | 'none'; + + export interface Layout { + visibility?: Visibility; + } + + export interface BackgroundLayout extends Layout { } export interface BackgroundPaint { @@ -1483,8 +1490,7 @@ declare namespace mapboxgl { 'background-opacity-transition'?: Transition; } - export interface FillLayout { - visibility?: 'visible' | 'none'; + export interface FillLayout extends Layout { } export interface FillPaint { @@ -1502,8 +1508,7 @@ declare namespace mapboxgl { 'fill-pattern-transition'?: Transition; } - export interface FillExtrusionLayout { - visibility?: 'visible' | 'none'; + export interface FillExtrusionLayout extends Layout { } export interface FillExtrusionPaint { @@ -1523,9 +1528,7 @@ declare namespace mapboxgl { 'fill-extrusion-vertical-gradient'?: boolean; } - export interface LineLayout { - visibility?: 'visible' | 'none'; - + export interface LineLayout extends Layout { 'line-cap'?: 'butt' | 'round' | 'square'; 'line-join'?: 'bevel' | 'round' | 'miter' | Expression; 'line-miter-limit'?: number | Expression; @@ -1555,9 +1558,7 @@ declare namespace mapboxgl { 'line-gradient'?: Expression; } - export interface SymbolLayout { - visibility?: 'visible' | 'none'; - + export interface SymbolLayout extends Layout { 'symbol-placement'?: 'point' | 'line' | 'line-center'; 'symbol-spacing'?: number | Expression; 'symbol-avoid-edges'?: boolean; @@ -1626,8 +1627,7 @@ declare namespace mapboxgl { 'text-translate-anchor'?: 'map' | 'viewport'; } - export interface RasterLayout { - visibility?: 'visible' | 'none'; + export interface RasterLayout extends Layout { } export interface RasterPaint { @@ -1647,8 +1647,7 @@ declare namespace mapboxgl { 'raster-resample'?: 'linear' | 'nearest'; } - export interface CircleLayout { - visibility?: 'visible' | 'none'; + export interface CircleLayout extends Layout { } export interface CirclePaint { @@ -1673,8 +1672,7 @@ declare namespace mapboxgl { 'circle-stroke-opacity-transition'?: Transition; } - export interface HeatmapLayout { - visibility?: 'visible' | 'none'; + export interface HeatmapLayout extends Layout { } export interface HeatmapPaint { @@ -1688,8 +1686,7 @@ declare namespace mapboxgl { 'heatmap-opacity-transition'?: Transition; } - export interface HillshadeLayout { - visibility?: 'visible' | 'none'; + export interface HillshadeLayout extends Layout { } export interface HillshadePaint { diff --git a/types/mapbox-gl/mapbox-gl-tests.ts b/types/mapbox-gl/mapbox-gl-tests.ts index ed5509ed70..3577d4e2b0 100644 --- a/types/mapbox-gl/mapbox-gl-tests.ts +++ b/types/mapbox-gl/mapbox-gl-tests.ts @@ -1008,3 +1008,16 @@ expectType(['coalesce', ['get', 'property'], ['get', 'prope */ expectType(new mapboxgl.Map().scrollZoom.setZoomRate(1)); expectType(new mapboxgl.Map().scrollZoom.setWheelZoomRate(1)); + +/* + * Visibility + */ +expectType('visible'); +expectType('none'); + +/* + * AnyLayout +*/ +expectType({visibility: 'none'}); +expectType({visibility: 'none', 'line-cap': 'round' }); +expectType({visibility: 'visible', 'icon-allow-overlap': true });