Skip to content

Commit

Permalink
incorrect compile time type for dictionary, fix #75 (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored Sep 29, 2017
1 parent b350f73 commit ca025d3
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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.

# 0.7.1

- **Bug Fix**
- incorrect compile time type for dictionary, fix #75 (@gcanti)

# 0.7.0

- **Breaking Change**
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": "0.7.0",
"version": "0.7.1",
"description": "TypeScript compatible runtime type system for IO validation",
"files": ["lib"],
"main": "lib/index.js",
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,10 @@ export const partial = <P extends Props>(props: P, name?: string): PartialType<P
// dictionaries
//

export class DictionaryType<D extends Type<string>, C extends Any> implements Type<{ [key: string]: TypeOf<C> }> {
export class DictionaryType<D extends Type<string>, C extends Any> implements Type<{ [K in TypeOf<D>]: TypeOf<C> }> {
readonly _tag: 'DictionaryType' = 'DictionaryType'
readonly _A: { [key: string]: TypeOf<C> }
readonly validate: Validate<{ [key: string]: TypeOf<C> }>
readonly _A: { [K in TypeOf<D>]: TypeOf<C> }
readonly validate: Validate<{ [K in TypeOf<D>]: TypeOf<C> }>
constructor(
readonly domain: D,
readonly codomain: C,
Expand Down
8 changes: 8 additions & 0 deletions test/dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ describe('dictionary', () => {
'Invalid value "s" supplied to : { [key: (string | <function1>)]: number }/aa: number'
])
})

it('should support literals as domain type', () => {
const D = t.dictionary(t.literal('foo'), t.string)
assertSuccess(t.validate({ foo: 'bar' }, D))
assertFailure(t.validate({ foo: 'bar', baz: 'bob' }, D), [
'Invalid value "baz" supplied to : { [key: "foo"]: string }/baz: "foo"'
])
})
})
19 changes: 19 additions & 0 deletions typings-checker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,25 @@ const D1 = t.dictionary(t.string, t.number)
const x12: TypeOf<typeof D1> = { a: 's' }
const x13: TypeOf<typeof D1> = { a: 1 }

const D2 = t.dictionary(t.literal('foo'), t.string)
const d1: t.TypeOf<typeof D2> = {
foo: 'bar'
}
const d2: t.TypeOf<typeof D2> = {
foo: 'bar',
// $ExpectError Object literal may only specify known properties, and 'baz' does not exist in type '{ foo: string; }'
baz: 'bob'
}
const D3 = t.dictionary(t.union([t.literal('foo'), t.literal('baz')]), t.string)
// $ExpectError Property 'baz' is missing in type '{ foo: string; }'
const d3: t.TypeOf<typeof D3> = {
foo: 'bar'
}
const d4: t.TypeOf<typeof D3> = {
foo: 'bar',
baz: 'bob'
}

//
// union
//
Expand Down

0 comments on commit ca025d3

Please sign in to comment.