Skip to content

Commit

Permalink
fix type information lost from nested decoder types, closes #479
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Jun 14, 2020
1 parent a1e46b4 commit 2aa88a8
Show file tree
Hide file tree
Showing 26 changed files with 198 additions and 54 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
**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.

# 2.2.5

- **Experimental**
- fix type information lost from nested decoder types, #479 (@gcanti)
- `JsonEncoder`
- change `id` signature (@gcanti)

# 2.2.4

- **Polish**
Expand Down
6 changes: 4 additions & 2 deletions docs/modules/Decoder.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,9 @@ Added in v2.2.0
**Signature**

```ts
export declare function partial<A>(properties: { [K in keyof A]: Decoder<A[K]> }): Decoder<Partial<A>>
export declare function partial<A>(
properties: { [K in keyof A]: Decoder<A[K]> }
): Decoder<Partial<{ [K in keyof A]: A[K] }>>
```

Added in v2.2.0
Expand Down Expand Up @@ -399,7 +401,7 @@ Added in v2.2.0
**Signature**

```ts
export declare function type<A>(properties: { [K in keyof A]: Decoder<A[K]> }): Decoder<A>
export declare function type<A>(properties: { [K in keyof A]: Decoder<A[K]> }): Decoder<{ [K in keyof A]: A[K] }>
```

Added in v2.2.0
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/Eq.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ Added in v2.2.2
**Signature**

```ts
export declare function partial<A>(properties: { [K in keyof A]: Eq<A[K]> }): Eq<Partial<A>>
export declare function partial<A>(properties: { [K in keyof A]: Eq<A[K]> }): Eq<Partial<{ [K in keyof A]: A[K] }>>
```

Added in v2.2.2
Expand Down Expand Up @@ -197,7 +197,7 @@ Added in v2.2.2
**Signature**

```ts
export declare const type: <A>(eqs: { [K in keyof A]: E.Eq<A[K]> }) => E.Eq<A>
export declare const type: <A>(eqs: { [K in keyof A]: E.Eq<A[K]> }) => E.Eq<{ [K in keyof A]: A[K] }>
```

Added in v2.2.2
6 changes: 4 additions & 2 deletions docs/modules/Guard.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ Added in v2.2.0
**Signature**

```ts
export declare function partial<A>(properties: { [K in keyof A]: Guard<A[K]> }): Guard<Partial<A>>
export declare function partial<A>(
properties: { [K in keyof A]: Guard<A[K]> }
): Guard<Partial<{ [K in keyof A]: A[K] }>>
```

Added in v2.2.0
Expand Down Expand Up @@ -262,7 +264,7 @@ Added in v2.2.0
**Signature**

```ts
export declare function type<A>(properties: { [K in keyof A]: Guard<A[K]> }): Guard<A>
export declare function type<A>(properties: { [K in keyof A]: Guard<A[K]> }): Guard<{ [K in keyof A]: A[K] }>
```

Added in v2.2.0
Expand Down
6 changes: 4 additions & 2 deletions docs/modules/JsonCodec.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ Added in v2.2.3
**Signature**
```ts
export declare const partial: <A>(properties: { [K in keyof A]: JsonCodec<A[K]> }) => JsonCodec<Partial<A>>
export declare const partial: <A>(
properties: { [K in keyof A]: JsonCodec<A[K]> }
) => JsonCodec<Partial<{ [K in keyof A]: A[K] }>>
```
Added in v2.2.3
Expand Down Expand Up @@ -266,7 +268,7 @@ Added in v2.2.3
**Signature**
```ts
export declare const type: <A>(properties: { [K in keyof A]: JsonCodec<A[K]> }) => JsonCodec<A>
export declare const type: <A>(properties: { [K in keyof A]: JsonCodec<A[K]> }) => JsonCodec<{ [K in keyof A]: A[K] }>
```
Added in v2.2.3
Expand Down
12 changes: 8 additions & 4 deletions docs/modules/JsonEncoder.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ Added in v2.2.3
**Signature**
```ts
export declare const id: JsonEncoder<Json>
export declare function id<A extends Json>(): JsonEncoder<A>
```

Added in v2.2.3
Added in v2.2.5

# intersection

Expand Down Expand Up @@ -182,7 +182,9 @@ Added in v2.2.3
**Signature**

```ts
export declare const partial: <A>(properties: { [K in keyof A]: JsonEncoder<A[K]> }) => JsonEncoder<Partial<A>>
export declare const partial: <A>(
properties: { [K in keyof A]: JsonEncoder<A[K]> }
) => JsonEncoder<Partial<{ [K in keyof A]: A[K] }>>
```

Added in v2.2.3
Expand Down Expand Up @@ -236,7 +238,9 @@ Added in v2.2.3
**Signature**

```ts
export declare const type: <A>(properties: { [K in keyof A]: JsonEncoder<A[K]> }) => JsonEncoder<A>
export declare const type: <A>(
properties: { [K in keyof A]: JsonEncoder<A[K]> }
) => JsonEncoder<{ [K in keyof A]: A[K] }>
```

Added in v2.2.3
8 changes: 4 additions & 4 deletions docs/modules/Schemable.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export interface Schemable<S> {
readonly number: HKT<S, number>
readonly boolean: HKT<S, boolean>
readonly nullable: <A>(or: HKT<S, A>) => HKT<S, null | A>
readonly type: <A>(properties: { [K in keyof A]: HKT<S, A[K]> }) => HKT<S, A>
readonly partial: <A>(properties: { [K in keyof A]: HKT<S, A[K]> }) => HKT<S, Partial<A>>
readonly type: <A>(properties: { [K in keyof A]: HKT<S, A[K]> }) => HKT<S, { [K in keyof A]: A[K] }>
readonly partial: <A>(properties: { [K in keyof A]: HKT<S, A[K]> }) => HKT<S, Partial<{ [K in keyof A]: A[K] }>>
readonly record: <A>(codomain: HKT<S, A>) => HKT<S, Record<string, A>>
readonly array: <A>(items: HKT<S, A>) => HKT<S, Array<A>>
readonly tuple: <A extends ReadonlyArray<unknown>>(...components: { [K in keyof A]: HKT<S, A[K]> }) => HKT<S, A>
Expand All @@ -62,8 +62,8 @@ export interface Schemable1<S extends URIS> {
readonly number: Kind<S, number>
readonly boolean: Kind<S, boolean>
readonly nullable: <A>(or: Kind<S, A>) => Kind<S, null | A>
readonly type: <A>(properties: { [K in keyof A]: Kind<S, A[K]> }) => Kind<S, A>
readonly partial: <A>(properties: { [K in keyof A]: Kind<S, A[K]> }) => Kind<S, Partial<A>>
readonly type: <A>(properties: { [K in keyof A]: Kind<S, A[K]> }) => Kind<S, { [K in keyof A]: A[K] }>
readonly partial: <A>(properties: { [K in keyof A]: Kind<S, A[K]> }) => Kind<S, Partial<{ [K in keyof A]: A[K] }>>
readonly record: <A>(codomain: Kind<S, A>) => Kind<S, Record<string, A>>
readonly array: <A>(items: Kind<S, A>) => Kind<S, Array<A>>
readonly tuple: <A extends ReadonlyArray<unknown>>(...components: { [K in keyof A]: Kind<S, A[K]> }) => Kind<S, A>
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/Type.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ Added in v2.2.3
**Signature**

```ts
export declare function partial<A>(properties: { [K in keyof A]: Type<A[K]> }): Type<Partial<A>>
export declare function partial<A>(properties: { [K in keyof A]: Type<A[K]> }): Type<Partial<{ [K in keyof A]: A[K] }>>
```

Added in v2.2.3
Expand Down Expand Up @@ -238,7 +238,7 @@ Added in v2.2.3
**Signature**

```ts
export declare function type<A>(properties: { [K in keyof A]: Type<A[K]> }): Type<A>
export declare function type<A>(properties: { [K in keyof A]: Type<A[K]> }): Type<{ [K in keyof A]: A[K] }>
```

Added in v2.2.3
Expand Down
17 changes: 17 additions & 0 deletions dtslint/ts3.5/Codec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as _ from '../../src/Codec'

// $ExpectType Codec<{ a: string; b: { c: number; }; }, { a: string; b: { c: number; }; }>
_.type({
a: _.string,
b: _.type({
c: _.number
})
})

// $ExpectType Codec<Partial<{ a: string; b: Partial<{ c: number; }>; }>, Partial<{ a: string; b: Partial<{ c: number; }>; }>>
_.partial({
a: _.string,
b: _.partial({
c: _.number
})
})
17 changes: 17 additions & 0 deletions dtslint/ts3.5/Decoder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as _ from '../../src/Decoder'

// $ExpectType Decoder<{ a: string; b: { c: number; }; }>
_.type({
a: _.string,
b: _.type({
c: _.number
})
})

// $ExpectType Decoder<Partial<{ a: string; b: Partial<{ c: number; }>; }>>
_.partial({
a: _.string,
b: _.partial({
c: _.number
})
})
11 changes: 5 additions & 6 deletions dtslint/ts3.5/Econder.ts → dtslint/ts3.5/Encoder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as E from '../../src/Encoder'
import { identity } from 'fp-ts/lib/function'

const NumberToString: E.Encoder<string, number> = {
encode: String
Expand All @@ -9,17 +8,17 @@ const BooleanToNumber: E.Encoder<number, boolean> = {
encode: (b) => (b ? 1 : 0)
}

export const Person = E.type({ name: E.categoryEncoder.id<string>(), age: NumberToString })
export const OfTest = E.type({ a: E.id<string>(), b: E.type({ c: NumberToString }) })

//
// TypeOf
//
export type Person = E.TypeOf<typeof Person> // $ExpectType { name: string; age: number; }
export type OfTest = E.TypeOf<typeof OfTest> // $ExpectType { a: string; b: { c: number; }; }

//
// OutputOf
//
export type PersonOut = E.OutputOf<typeof Person> // $ExpectType { name: string; age: string; }
export type OfTestOutput = E.OutputOf<typeof OfTest> // $ExpectType { a: string; b: { c: string; }; }

//
// nullable
Expand All @@ -29,12 +28,12 @@ E.nullable(NumberToString) // $ExpectType Encoder<string | null, number | null>
//
// type
//
E.type({ a: NumberToString }) // $ExpectType Encoder<{ a: string; }, { a: number; }>
E.type({ a: E.type({ b: NumberToString }) }) // $ExpectType Encoder<{ a: { b: string; }; }, { a: { b: number; }; }>

//
// partial
//
E.partial({ a: NumberToString }) // $ExpectType Encoder<Partial<{ a: string; }>, Partial<{ a: number; }>>
E.partial({ a: E.partial({ b: NumberToString }) }) // $ExpectType Encoder<Partial<{ a: Partial<{ b: string; }>; }>, Partial<{ a: Partial<{ b: number; }>; }>>

//
// record
Expand Down
17 changes: 17 additions & 0 deletions dtslint/ts3.5/Eq.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as _ from '../../src/Eq'

// $ExpectType Eq<{ a: string; b: { c: number; }; }>
_.type({
a: _.string,
b: _.type({
c: _.number
})
})

// $ExpectType Eq<Partial<{ a: string; b: Partial<{ c: number; }>; }>>
_.partial({
a: _.string,
b: _.partial({
c: _.number
})
})
17 changes: 17 additions & 0 deletions dtslint/ts3.5/Guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as _ from '../../src/Guard'

// $ExpectType Guard<{ a: string; b: { c: number; }; }>
_.type({
a: _.string,
b: _.type({
c: _.number
})
})

// $ExpectType Guard<Partial<{ a: string; b: Partial<{ c: number; }>; }>>
_.partial({
a: _.string,
b: _.partial({
c: _.number
})
})
17 changes: 17 additions & 0 deletions dtslint/ts3.5/JsonCodec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as _ from '../../src/JsonCodec'

// $ExpectType JsonCodec<{ a: string; b: { c: number; }; }>
_.type({
a: _.string,
b: _.type({
c: _.number
})
})

// $ExpectType JsonCodec<Partial<{ a: string; b: Partial<{ c: number; }>; }>>
_.partial({
a: _.string,
b: _.partial({
c: _.number
})
})
17 changes: 17 additions & 0 deletions dtslint/ts3.5/JsonEncoder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as _ from '../../src/JsonEncoder'

// $ExpectType JsonEncoder<{ a: string; b: { c: number; }; }>
_.type({
a: _.id<string>(),
b: _.type({
c: _.id<number>()
})
})

// $ExpectType JsonEncoder<Partial<{ a: string; b: Partial<{ c: number; }>; }>>
_.partial({
a: _.id<string>(),
b: _.partial({
c: _.id<number>()
})
})
9 changes: 4 additions & 5 deletions dtslint/ts3.5/Schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as D from '../../src/Decoder'
import { Schemable, WithUnknownContainers, memoize, WithRefinement, WithUnion } from '../../src/Schemable'
import { HKT } from 'fp-ts/lib/HKT'

Expand All @@ -15,8 +14,8 @@ function make<A>(f: Schema<A>): Schema<A> {
//
// TypeOf
//
export const Person = make((S) => S.type({ name: S.string, age: S.number }))
export type Person = TypeOf<typeof Person> // $ExpectType { name: string; age: number; }
export const OfTest = make((S) => S.type({ a: S.string, b: S.type({ c: S.number }) }))
export type OfTest = TypeOf<typeof OfTest> // $ExpectType { a: string; b: { c: number; }; }

//
// literal
Expand Down Expand Up @@ -46,12 +45,12 @@ make((S) => S.nullable(S.string)) // $ExpectType Schema<string | null>
//
// type
//
make((S) => S.type({ a: S.string })) // $ExpectType Schema<{ a: string; }>
make((S) => S.type({ a: S.string, b: S.type({ c: S.number }) })) // $ExpectType Schema<{ a: string; b: { c: number; }; }>

//
// partial
//
make((S) => S.partial({ a: S.string })) // $ExpectType Schema<Partial<{ a: string; }>>
make((S) => S.partial({ a: S.string, b: S.partial({ c: S.number }) })) // $ExpectType Schema<Partial<{ a: string; b: Partial<{ c: number; }>; }>>

//
// record
Expand Down
17 changes: 17 additions & 0 deletions dtslint/ts3.5/Type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as _ from '../../src/Type'

// $ExpectType Type<{ a: string; b: { c: number; }; }>
_.type({
a: _.string,
b: _.type({
c: _.number
})
})

// $ExpectType Type<Partial<{ a: string; b: Partial<{ c: number; }>; }>>
_.partial({
a: _.string,
b: _.partial({
c: _.number
})
})
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": "2.2.4",
"version": "2.2.5",
"description": "TypeScript runtime type system for IO decoding/encoding",
"files": [
"lib",
Expand Down
4 changes: 3 additions & 1 deletion src/Codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ export function type<P extends Record<string, Codec<any, any>>>(
export function partial<P extends Record<string, Codec<any, any>>>(
properties: P
): Codec<Partial<{ [K in keyof P]: OutputOf<P[K]> }>, Partial<{ [K in keyof P]: TypeOf<P[K]> }>> {
return make(D.partial(properties), E.partial(properties))
// these tow `any`s are required to make typescript@3.5 compile
// vvvvvv vvvvvv
return make(D.partial(properties as any), E.partial(properties)) as any
}

/**
Expand Down
Loading

0 comments on commit 2aa88a8

Please sign in to comment.