diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c5b71c1a..1bfff7356 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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** diff --git a/package.json b/package.json index 5ba9221ba..e0e04f718 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index 9d0213713..ca44495bc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -363,10 +363,10 @@ export const partial =

(props: P, name?: string): PartialType

, C extends Any> implements Type<{ [key: string]: TypeOf }> { +export class DictionaryType, C extends Any> implements Type<{ [K in TypeOf]: TypeOf }> { readonly _tag: 'DictionaryType' = 'DictionaryType' - readonly _A: { [key: string]: TypeOf } - readonly validate: Validate<{ [key: string]: TypeOf }> + readonly _A: { [K in TypeOf]: TypeOf } + readonly validate: Validate<{ [K in TypeOf]: TypeOf }> constructor( readonly domain: D, readonly codomain: C, diff --git a/test/dictionary.ts b/test/dictionary.ts index d64743b53..beb7b9b0b 100644 --- a/test/dictionary.ts +++ b/test/dictionary.ts @@ -29,4 +29,12 @@ describe('dictionary', () => { 'Invalid value "s" supplied to : { [key: (string | )]: 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"' + ]) + }) }) diff --git a/typings-checker/index.ts b/typings-checker/index.ts index 354d93de8..78242ba7e 100644 --- a/typings-checker/index.ts +++ b/typings-checker/index.ts @@ -83,6 +83,25 @@ const D1 = t.dictionary(t.string, t.number) const x12: TypeOf = { a: 's' } const x13: TypeOf = { a: 1 } +const D2 = t.dictionary(t.literal('foo'), t.string) +const d1: t.TypeOf = { + foo: 'bar' +} +const d2: t.TypeOf = { + 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 = { + foo: 'bar' +} +const d4: t.TypeOf = { + foo: 'bar', + baz: 'bob' +} + // // union //