diff --git a/types/mapbox-gl/index.d.ts b/types/mapbox-gl/index.d.ts index 149d433a89..786b7260b6 100644 --- a/types/mapbox-gl/index.d.ts +++ b/types/mapbox-gl/index.d.ts @@ -1,8 +1,10 @@ // Type definitions for Mapbox GL JS v0.51.0 // Project: https://github.com/mapbox/mapbox-gl-js -// Definitions by: Dominik Bruderer , Patrick Reames +// Definitions by: Dominik Bruderer +// Patrick Reames +// Karl-Aksel Puulmann // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 +// TypeScript Version: 3.0 /// @@ -20,7 +22,33 @@ declare namespace mapboxgl { type LngLatLike = LngLat | { lng: number; lat: number; } | { lon: number; lat: number; } | [number, number]; type LngLatBoundsLike = LngLatBounds | [LngLatLike, LngLatLike] | [number, number, number, number]; type PointLike = Point | [number, number]; - type Expression = any[]; + + type ExpressionName = + // Types + | 'array' | 'boolean' | 'collator' | 'format' | 'literal' | 'number' | 'object' | 'string' + | 'to-boolean' | 'to-color' | 'to-number' | 'to-string' | 'typeof' + // Feature data + | 'feature-state' | 'geometry-type' | 'id' | 'line-progress' | 'properties' + // Lookup + | 'at' | 'get' | 'has' | 'length' + // Decision + | '!=' | '<' | '<=' | '==' | '>' | '>=' | 'all' | 'any' | 'case' | 'match' + // Ramps, scales, curves + | 'interpolate' | 'interpolate-hcl' | 'interpolate-lab' | 'step' + // Variable binding + | 'let' | 'var' + // String + | 'concat' | 'downcase' | 'is-supported-script' | 'resolved-locale' | 'upcase' + // Color + | 'rgb' | 'rgba' + // Math + | '-' | '*' | '/' | '%' | '^' | '+' | 'abs' | 'acos' | 'asin' | 'atan' | 'ceil' | 'cos' | 'e' + | 'floor' | 'ln' | 'ln2' | 'log10' | 'log2' | 'max' | 'min' | 'pi' | 'round' | 'sin' | 'sqrt' | 'tan' + // Zoom, Heatmap + | 'zoom' | 'heatmap-density'; + + type Expression = [ExpressionName, ...any[]] + type Anchor = 'center' | 'left' | 'right' | 'top' | 'bottom' | 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'; /** @@ -73,7 +101,7 @@ declare namespace mapboxgl { isStyleLoaded(): boolean; - addSource(id: string, source: VectorSource | RasterSource | RasterDemSource | GeoJSONSource | ImageSource | VideoSource | GeoJSONSourceRaw): this; + addSource(id: string, source: AnySourceData): this; isSourceLoaded(id: string): boolean; @@ -81,7 +109,7 @@ declare namespace mapboxgl { removeSource(id: string): this; - getSource(id: string): VectorSource | RasterSource | RasterDemSource | GeoJSONSource | ImageSource | VideoSource; + getSource(id: string): AnySourceImpl; addImage(name: string, image: HTMLImageElement | ArrayBufferView | { width: number, height: number, data: Uint8Array | Uint8ClampedArray } | ImageData, options?: { pixelRatio?: number, sdf?: boolean }): this; @@ -626,7 +654,7 @@ declare namespace mapboxgl { name?: string; pitch?: number; light?: Light; - sources?: any; + sources?: Sources; sprite?: string; transition?: Transition; version: number; @@ -648,6 +676,14 @@ declare namespace mapboxgl { 'intensity-transition'?: Transition; } + export interface Sources { + [sourceName: string]: AnySourceData; + } + + export type AnySourceData = GeoJSONSourceRaw | VideoSourceRaw | ImageSourceRaw | CanvasSourceRaw | VectorSource | RasterSource | RasterDemSource + + export type AnySourceImpl = GeoJSONSource | VideoSource | ImageSource | CanvasSource | VectorSource | RasterSource | RasterDemSource + export interface Source { type: 'vector' | 'raster' | 'raster-dem' | 'geojson' | 'image' | 'video' | 'canvas'; } @@ -699,10 +735,11 @@ declare namespace mapboxgl { /** * VideoSource */ - export interface VideoSource extends VideoSourceOptions { + export interface VideoSourceRaw extends Source, VideoSourceOptions { + type: 'video'; } - export class VideoSource implements Source { + export class VideoSource implements VideoSourceRaw { type: 'video'; constructor(options?: mapboxgl.VideoSourceOptions); @@ -721,10 +758,11 @@ declare namespace mapboxgl { /** * ImageSource */ - export interface ImageSource extends ImageSourceOptions { + export interface ImageSourceRaw extends Source, ImageSourceOptions { + type: 'image'; } - export class ImageSource implements Source { + export class ImageSource implements ImageSourceRaw { type: 'image'; constructor(options?: mapboxgl.ImageSourceOptions); @@ -741,7 +779,11 @@ declare namespace mapboxgl { /** * CanvasSource */ - export class CanvasSource implements Source, CanvasSourceOptions { + export interface CanvasSourceRaw extends Source, CanvasSourceOptions { + type: 'canvas'; + } + + export class CanvasSource implements CanvasSourceRaw { type: 'canvas'; coordinates: number[][]; @@ -1246,7 +1288,7 @@ declare namespace mapboxgl { metadata?: any; ref?: string; - source?: string | VectorSource | RasterSource | RasterDemSource | GeoJSONSource | ImageSource | VideoSource | GeoJSONSourceRaw; + source?: string | AnySourceData; 'source-layer'?: string; @@ -1361,7 +1403,7 @@ declare namespace mapboxgl { 'symbol-spacing'?: number | Expression; 'symbol-avoid-edges'?: boolean; 'symbol-z-order'?: 'viewport-y' | 'source'; - 'icon-allow-overlap'?: boolean | StyleFunction; + 'icon-allow-overlap'?: boolean | StyleFunction | Expression; 'icon-ignore-placement'?: boolean; 'icon-optional'?: boolean; 'icon-rotation-alignment'?: 'map' | 'viewport' | 'auto'; @@ -1377,7 +1419,7 @@ declare namespace mapboxgl { 'icon-pitch-alignment'?: 'map' | 'viewport' | 'auto'; 'text-pitch-alignment'?: 'map' | 'viewport' | 'auto'; 'text-rotation-alignment'?: 'map' | 'viewport' | 'auto'; - 'text-field'?: string | StyleFunction; + 'text-field'?: string | StyleFunction | Expression; 'text-font'?: string | string[]; 'text-size'?: number | StyleFunction | Expression; 'text-max-width'?: number | Expression; diff --git a/types/mapbox-gl/mapbox-gl-tests.ts b/types/mapbox-gl/mapbox-gl-tests.ts index d119c76bd5..92f0c6c7c7 100644 --- a/types/mapbox-gl/mapbox-gl-tests.ts +++ b/types/mapbox-gl/mapbox-gl-tests.ts @@ -50,7 +50,8 @@ map.on('load', function(){ "type": "symbol", "source": "data", "layout": { - "icon-image": "marker-15" + "icon-image": "marker-15", + "text-field": ['get', 'property'] } }); @@ -788,3 +789,14 @@ expectType(map.on('touchcancel', 'text', (ev) => { expectType(ev); expectType(ev.features); })); + +/* + * Expression + */ +expectType(['id']); +expectType(['get', 'property']); +expectType([ + 'format', + ['concat', ['get', 'name'], '\n'], {}, + ['concat', ['get', 'area'], 'foobar', { 'font-scale': 0.8 }] +]); diff --git a/types/react-map-gl/index.d.ts b/types/react-map-gl/index.d.ts index 772e24081f..4d147d3611 100644 --- a/types/react-map-gl/index.d.ts +++ b/types/react-map-gl/index.d.ts @@ -3,7 +3,7 @@ // Definitions by: Robert Imig // Fabio Berta // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.8 +// TypeScript Version: 3.0 import * as React from 'react'; import * as MapboxGL from 'mapbox-gl';