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
import*astfrom'io-ts';import{either,left,right}from'fp-ts/Either';import*asEitherfrom'fp-ts/Either';import{PathReporter}from'io-ts/lib/PathReporter';import{identity}from'fp-ts/function';typeStringWithUnderscore= `_${string}`;constStringWithUnderscore=newt.Type<StringWithUnderscore,unknown,unknown>('StringWithUnderscore',(u): u is StringWithUnderscore=>t.string.is(u)&&0<u.length&&u.startsWith('_'),(u,c)=>either.chain(t.string.validate(u,c),s=>0<s.length&&s.startsWith('_') ? t.success(sasStringWithUnderscore) : t.failure(u,c)),identity,);describe('intersection with record whose keys are of type StringWithUnderscore',()=>{it('typechecks but does not pass validation',()=>{constBar=t.intersection([t.type({foo: t.string}),t.record(StringWithUnderscore,t.number)]);typeBar=t.TypeOf<typeofBar>;constbar: Bar={foo: '','_foo': 123};expect(()=>decodeOrThrow(Bar,bar)).toThrow();});});exportfunctiondecodeOrThrow<A,O,I>(typ: t.Type<A,O,I>,i: I): A{returnEither.getOrElse<t.Errors,A>((err)=>{thrownewError(PathReporter.report(Either.left(err)).join('\n'));})(typ.decode(i));}
Bar.decode(bar) fails with
Invalid value "foo" supplied to : ({ foo: string } & { [K in StringWithUnderscore]: number })/1: { [K in StringWithUnderscore]: number }/foo: StringWithUnderscore
Expected behavior
Bar.decode(bar) should succeed
Reproducible example
See above
Suggested solution(s)
Additional context
I tried with a literal and that worked:
describe('intersection with record that has a literal as type',()=>{it('type-checks and passes validation',()=>{constFoo=t.intersection([t.type({foo: t.string}),t.record(t.literal('bar'),t.number)]);typeFoo=t.TypeOf<typeofFoo>;constfoo: Foo={foo: '',bar: 123};expect(Foo.decode(foo)).toEqual(right(foo));});});
Your environment
Software
Version(s)
io-ts
2.2.20
fp-ts
2.13.1
TypeScript
4.9.4
The text was updated successfully, but these errors were encountered:
🐛 Bug report
Current Behavior
Bar.decode(bar)
fails withExpected behavior
Bar.decode(bar)
should succeedReproducible example
See above
Suggested solution(s)
Additional context
I tried with a literal and that worked:
Your environment
The text was updated successfully, but these errors were encountered: