You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Current Behavior & Expected behavior & Reproducible example
import*astfrom'io-ts';constA=t.type({A: t.union([t.number,t.undefined,t.null]),});constB=t.type({B: t.union([t.number,t.undefined,t.null]),});constC=t.partial({C: t.union([t.number,t.null]),});constUnionAB=t.union([A,B]);constUnionBA=t.union([B,A]);constUnionAC=t.union([A,C]);constUnionCA=t.union([C,A]);consttarget={A: 1};{constres=UnionAB.decode(target);if(isRight(res)){console.log(res.right);// { A: 1 } <- okconsole.log(A.is(res.right));// true <- okconsole.log(B.is(res.right));// false <- ok}}{constres=UnionBA.decode(target);if(isRight(res)){console.log(res.right);// { A: 1, B: undefined } <- NG (expected to be { A: 1 })console.log(A.is(res.right));// true <- okconsole.log(B.is(res.right));// true <- NG (expected to be false)}}{constres=UnionCA.decode(target);if(isRight(res)){console.log(res.right);// { A: 1 } <- NG (expected to be {})console.log(A.is(res.right));// true <- okconsole.log(C.is(res.right));// true <- ok}}{constres=UnionAC.decode(target);if(isRight(res)){console.log(res.right);// { A: 1 }console.log(A.is(res.right));// true <- okconsole.log(C.is(res.right));// true <- ok}}
Suggested solution(s)
I think io-ts should distinguish between t.partial({ a: t.number }) and t.type({ a: t.union([t.number, t.undefined]) }) by using Object.hasOwnProperty in addition to checking if the key's value is undefined.
Additional context
Your environment
Which versions of io-ts are affected by this issue? Did this work in previous versions of io-ts?
Software
Version(s)
io-ts
2.2.16
fp-ts
2.11.8
TypeScript
4.5.5
The text was updated successfully, but these errors were encountered:
noshiro-pf
changed the title
union of union with Undefined
Bad decode result when using union and undefined
Nov 29, 2022
🐛 Bug report
Current Behavior & Expected behavior & Reproducible example
Suggested solution(s)
I think io-ts should distinguish between
t.partial({ a: t.number })
andt.type({ a: t.union([t.number, t.undefined]) })
by usingObject.hasOwnProperty
in addition to checking if the key's value is undefined.Additional context
Your environment
Which versions of io-ts are affected by this issue? Did this work in previous versions of io-ts?
The text was updated successfully, but these errors were encountered: