-
-
Notifications
You must be signed in to change notification settings - Fork 30
Description
Bug
Describe the bug
There is an incorrect type-definition of the readStyle(olStyle) function input param "olStyle". The current code states that the input parametre is of type OlStyleLike, which imports directly StyleLike as OlStyleLike from ol/style/Style. This is untrue, as the StyleLike type as defined by OL is Style | Array<Style> | StyleFunction, of which StyleFunction is a function with a signature (FeatureLike, number) => Style | Array<Style> | void. This breaks the actual expectations of the readStyle(olStyle) function, which does not work with StyleFunction, but actually expects an OlParserStyleFct (beside the Style and Array<Style> which do match their definitions by OL).
To Reproduce
Steps to reproduce the behavior:
import {Fill, Style} from 'ol/style';
import {FeatureLike} from 'ol/Feature';
const style = (feature: FeatureLike, resolution: number) => {
return new Style({
fill: new Fill({
color: [255, 255, 255, 0.5],
}),
}),
};
const {OlStyleParser} = await import('geostyler-openlayers-parser');
const parser = new OlStyleParser();
const {output: geoStylerStyle} = await parser.readStyle(style);
console.log(geoStylerStyle); // undefined
Expected behavior
The problem is twofold:
- the
readStyle(olStyle)function shall have a proper type definition of its param asreadStyle(olStyle: OlStyle | OlStyle[] | OlParserStyleFct). - the
isOlParserStyleFct()check shall be improved so it also checks for a presence of the specific '__geoStylerStyle' property in the function object (typeof x === 'function' && '__geoStylerStyle' in x).
The expected behaviour is the real question here. At first, I would expect TypeScript to prevent me passing an incorrect object (or function) into the function call.
Screenshots
If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
- OS: irrelevant
- Browser: irrelevant
- Version: irrelevant
Additional context
OlParserStyleFct was originally introduced in #39 along with the isOlParserStyleFct() check. The types of input params of readStyle(olStyle) were correct back then: OlStyle | OlStyle[] | OlParserStyleFct. This was later softened to any by this commit and even later retyped to OlStyleLike in this commit. Since Sep 30, 2021, it lives there untouched, which seems surprising that it was not causing any issue to anyone yet.