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

New Source types in v3.5 don't support type narrowing via the type property #13220

Closed
ezzatron opened this issue Jul 12, 2024 · 1 comment
Closed
Labels
bug 🐞 types Issues related to the public TypeScript typings

Comments

@ezzatron
Copy link

Related to #13203

mapbox-gl-js version: v3.5.1

browser: N/A

Steps to Trigger Behavior

  1. Write code that checks the type property of a Source to narrow its type.
  2. Try to use the narrowed type source as the expected type.
  3. Source type is not narrowed.

Link to Demonstration

https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgcQKYQFIGUDyA5LCAVygGNUAaOAWQEMwBfOAMyghDgCIR6AjCAB4BaAOYAbTgG4AUNNIQAdgGd4PMHAC8cBagDuNegAoE8hTFrAdUAFxwAJhFJEQqMwDoRqGAFExqF2YAQgCeAJJ2hpzAdpwAlACEDLEypipwSsRkqJpwah5ehCTkkdFxMtJ+8J6YuASZ5LZoNfiFWeXAzHCGGUWoAPxuMMFg2RpjXNUAVhkKcYjScItw1dgt9aPp6zIMQA

Repeated here:

import { GeoJSONSource, Map} from "mapbox-gl";

const map = new Map({container: document.getElementById("id")!});
const source = map.getSource("id");

let geoJSONSource: GeoJSONSource;

if (source?.type === "geojson") {
    geoJSONSource = source; // <-- this should not be a type error, but it is
}

Expected Behavior

You can narrow a Source type to a GeoJSONSource type by using source.type === "geojson".

Actual Behavior

Type 'GeoJSONSource | VideoSource | ImageSource | CanvasSource' is not assignable to type 'GeoJSONSource'.

@stepankuzmin
Copy link
Contributor

Thanks for creating an issue, @ezzatron. We'll address this in the upcoming patch release.

@stepankuzmin stepankuzmin added the types Issues related to the public TypeScript typings label Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐞 types Issues related to the public TypeScript typings
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants