Global state value becomes `undefined` when not set but other keys are set
bug
**Describe the bug**
When using the `["global-state", key]` expression with a key that has not been set yet, and if any other keys _have_ been set, the value is resolved as `undefined`.
**To Reproduce**
I made a tiny reproduction at https://github.com/Alduino/maplibre-global-state-reprod, you can run it with `pnpm run dev`.
It just sets the text colour to red if `["==", ["global-state", "test"], null]` or blue otherwise.
1. Text initially is red; global state object is empty
2. Click `Set "unrelated" to true` which sets a different global state key to a value; text becomes blue due to the resolved value for `test` becoming `undefined`
3. Click `Set "test" to null` to explicitly set the test global state value to null; text becomes red again.
Or if it's easier to read pseudocode:
```js
evaluate(["==", ["global-state", "test"], null]); // null
map.setGlobalState("unrelated", true);
evaluate(["==", ["global-state", "test"], null]); // undefined(!)
map.setGlobalState("test", null);
evaluate(["==", ["global-state", "test"], null]); // back to null
```
**Expected behavior**
As [described in the docs](https://maplibre.org/maplibre-style-spec/expressions/#global-state) the global state expression should return `null` if the value isn't set.
**Screenshots**
| | | |
|---|---|---|
| <img width="600" height="735" alt="Image" src="https://github.com/user-attachments/assets/d2676a84-3e06-49af-a852-ccd58cd87eb5" /> | <img width="600" height="784" alt="Image" src="https://github.com/user-attachments/assets/be92f7af-0010-4068-bead-7c301905d7b3" /> | <img width="600" height="810" alt="Image" src="https://github.com/user-attachments/assets/ee54466d-9343-4c40-8a60-0751e5c11764" /> |
**Desktop (please complete the following information):**
- OS: Fedora 43 KDE Plasma Edition
- Browser: Firefox 148
- Version: maplibre-gl v5.19.0, @maplibre/maplibre-gl-style-spec 24.6.0
**Smartphone (please complete the following information):**
- N/A
**Additional context**
Not sure if this should be posted here or in the maplibre-gl-js repo, issue seems to be from the code in this repo though.
As far as I can tell it's coming from here:
- https://github.com/maplibre/maplibre-style-spec/blob/8d85feb97dbe27112f4504ee6317d3e0b55bfed7/src/expression/definitions/global_state.ts#L38-L44
- https://github.com/maplibre/maplibre-style-spec/blob/8d85feb97dbe27112f4504ee6317d3e0b55bfed7/src/util/get_own.ts#L13-L15
If the global state object isn't defined or has no keys then `null` is returned, otherwise `getOwn` returns `undefined` instead of `null`.
Maybe `evaluate` could return `getOwn() ?? null`? That would also return `null` even if the value is explicitly set to `undefined`, although seeing as afaict in the style spec `undefined` basically doesn't exist maybe that would make sense too?
关闭于 2026-06-07 3 条评论