Skip to content

Commit

Permalink
Polish: make isIndexableCodec more strict
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Jan 15, 2019
1 parent b97d62a commit 139200e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
**Note**: Gaps between patch versions are faulty/broken releases. **Note**: A feature tagged as Experimental is in a
high state of flux, you're at risk of it changing without notice.

# 1.6.2

- **Polish**
- make `isIndexableCodec` more strict (@gcanti)

# 1.6.1

- **Bug Fix**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "io-ts",
"version": "1.6.1",
"version": "1.6.2",
"description": "TypeScript compatible runtime type system for IO validation",
"files": [
"lib"
Expand Down
11 changes: 5 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1601,12 +1601,11 @@ const getCodecIndexRecord = (codec: Mixed, override: Mixed = codec): IndexRecord

const isIndexableCodec = (codec: Mixed): boolean => {
return (
isInterfaceCodec(codec) ||
isExactCodec(codec) ||
isIntersectionCodec(codec) ||
isUnionCodec(codec) ||
isStrictCodec(codec) ||
isRefinementCodec(codec) ||
((isInterfaceCodec(codec) || isStrictCodec(codec)) &&
Object.keys(codec.props).some(key => isLiteralCodec(codec.props[key]))) ||
((isExactCodec(codec) || isRefinementCodec(codec)) && isIndexableCodec(codec.type)) ||
(isIntersectionCodec(codec) && codec.types.some(isIndexableCodec)) ||
(isUnionCodec(codec) && codec.types.every(isIndexableCodec)) ||
isRecursiveCodec(codec)
)
}
Expand Down
20 changes: 20 additions & 0 deletions test/taggedUnion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,24 @@ describe('getIndexRecord', () => {
kind: [['C2', C], ['D2', D]]
})
})

it('should handle recursive types defined with a union containing a non indexable codec', () => {
// non indexable codec
const A = t.type({
a: t.string
})

type A = t.TypeOf<typeof A>

interface B {
modules: A | B
}

const B = t.recursion<B>('B', self =>
t.type({
modules: t.union([A, self])
})
)
assert.deepEqual(t.getIndexRecord([B]), {})
})
})

0 comments on commit 139200e

Please sign in to comment.