gcanti / io-ts

Runtime type system for IO decoding/encoding
https://gcanti.github.io/io-ts/
MIT License
6.71k stars 329 forks source link

Incorrect behaviour when used with browser geolocation coordinates object #711

Open dearlordylord opened 8 months ago

dearlordylord commented 8 months ago

🐛 Bug report

Current Behavior

When you parse the result of navigator.geolocation.getCurrentPosition, despite coords object having field values, a codec parsing these values returns them successfully unassigned, e.g. an empty object, even when the values are required

Expected behavior

for codec

const geoCoordinatesCodec = t.type({
  latitude: t.number,
  longitude: t.number,
});

and


navigator.geolocation.getCurrentPosition((position) => {

  const result = geoCoordinatesCodec.decode(position.coords);

});

result has to be {right: {latitude: ..., longitude: ...}}

instead, the result is {right: {}}

Reproducible example

https://github.com/Firfi/io-ts-geo-repro

Suggested solution(s)

I expect io-ts either return parse errors or correct value

Additional context

It seems to be some special object proxying / getters involved in location.coords object

if I do

 const result = geoCoordinatesCodec.decode({
  latitude: position.coords.latitude,
  longitude: position.coords.longitude
});

it works as expected

Your environment

macos + chrome,

osx + safari

https://github.com/Firfi/io-ts-geo-repro - latest

Software Version(s)
io-ts 2.2.21
fp-ts 2.16.2
TypeScript 5.3.2