Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type narrowing of image source types #13221

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/source/canvas_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export type CanvasSourceSpecification = {
* map.removeSource('some id'); // remove
* @see [Example: Add a canvas source](https://docs.mapbox.com/mapbox-gl-js/example/canvas-source/)
*/
class CanvasSource extends ImageSource {
class CanvasSource extends ImageSource<'canvas'> {
options: CanvasSourceSpecification;
animate: boolean;
canvas: HTMLCanvasElement;
Expand All @@ -67,6 +67,7 @@ class CanvasSource extends ImageSource {
*/
constructor(id: string, options: CanvasSourceSpecification, dispatcher: Dispatcher, eventedParent: Evented) {
super(id, options, dispatcher, eventedParent);
this.type = 'canvas';

// We build in some validation here, since canvas sources aren't included in the style spec:
if (!options.coordinates) {
Expand Down
6 changes: 3 additions & 3 deletions src/source/image_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ function sortTriangles(centerLatitudes: number[], indices: TriangleIndexArray):
* @see [Example: Add an image](https://www.mapbox.com/mapbox-gl-js/example/image-on-a-map/)
* @see [Example: Animate a series of images](https://www.mapbox.com/mapbox-gl-js/example/animate-images/)
*/
class ImageSource extends Evented implements ISource {
type: string;
class ImageSource<T extends 'canvas' | 'image' | 'video' = 'image'> extends Evented implements ISource {
type: T;
id: string;
scope: string;
minzoom: number;
Expand Down Expand Up @@ -276,7 +276,7 @@ class ImageSource extends Evented implements ISource {
this.dispatcher = dispatcher;
this.coordinates = options.coordinates;

this.type = 'image';
this.type = 'image' as T;
this.minzoom = 0;
this.maxzoom = 22;
this.tileSize = 512;
Expand Down
1 change: 0 additions & 1 deletion src/source/source_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ class SourceCache extends Evented {
this._isRaster =
this._source.type === 'raster' ||
this._source.type === 'raster-dem' || this._source.type === 'raster-array' ||
// @ts-expect-error - TS2339 - Property '_dataType' does not exist on type 'VideoSource | ImageSource | CanvasSource | CustomSource<ImageBitmap | HTMLCanvasElement | HTMLImageElement | ImageData>'.
(this._source.type === 'custom' && this._source._dataType === 'raster');
}

Expand Down
2 changes: 1 addition & 1 deletion src/source/video_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import type {VideoSourceSpecification} from '../style-spec/types';
* map.removeSource('some id'); // remove
* @see [Example: Add a video](https://www.mapbox.com/mapbox-gl-js/example/video-on-a-map/)
*/
class VideoSource extends ImageSource {
class VideoSource extends ImageSource<'video'> {
options: VideoSourceSpecification;
urls: Array<string>;
video: HTMLVideoElement;
Expand Down