Skip to content

Commit 9f4daf0

Browse files
committed
deprecate taggedUnion in favour of union
1 parent 3bd3d43 commit 9f4daf0

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ high state of flux, you're at risk of it changing without notice.
1818

1919
- **New Feature**
2020
- `union` is now able to detect and optimize tagged unions (@gcanti)
21+
- **Deprecation**
22+
- deprecate `taggedUnion` in favour of `union` (@gcanti)
2123

2224
# 1.8.6
2325

docs/modules/index.ts.md

+13-7
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ parent: Modules
4646
- [~~TaggedIntersection~~ (interface)](#taggedintersection-interface)
4747
- [~~TaggedRefinement~~ (interface)](#taggedrefinement-interface)
4848
- [~~TaggedUnion~~ (interface)](#taggedunion-interface)
49-
- [TaggedUnionC (interface)](#taggedunionc-interface)
49+
- [~~TaggedUnionC~~ (interface)](#taggedunionc-interface)
5050
- [TupleC (interface)](#tuplec-interface)
5151
- [TypeC (interface)](#typec-interface)
5252
- [UndefinedC (interface)](#undefinedc-interface)
@@ -103,7 +103,7 @@ parent: Modules
103103
- [RefinementType (class)](#refinementtype-class)
104104
- [~~StrictType~~ (class)](#stricttype-class)
105105
- [StringType (class)](#stringtype-class)
106-
- [TaggedUnionType (class)](#taggeduniontype-class)
106+
- [~~TaggedUnionType~~ (class)](#taggeduniontype-class)
107107
- [TupleType (class)](#tupletype-class)
108108
- [Type (class)](#type-class)
109109
- [pipe (method)](#pipe-method)
@@ -154,7 +154,7 @@ parent: Modules
154154
- [~~refinement~~ (function)](#refinement-function)
155155
- [strict (function)](#strict-function)
156156
- [success (function)](#success-function)
157-
- [taggedUnion (function)](#taggedunion-function)
157+
- [~~taggedUnion~~ (function)](#taggedunion-function)
158158
- [tuple (function)](#tuple-function)
159159
- [type (function)](#type-function)
160160
- [union (function)](#union-function)
@@ -600,12 +600,12 @@ export interface TaggedUnion<Tag extends string, A, O = A> extends UnionType<Arr
600600

601601
Added in v1.3.0
602602

603-
# TaggedUnionC (interface)
603+
# ~~TaggedUnionC~~ (interface)
604604

605605
**Signature**
606606

607607
```ts
608-
export interface TaggedUnionC<Tag extends string, CS extends [Mixed, Mixed, ...Array<Mixed>]>
608+
export interface TaggedUnionC<Tag extends string, CS extends [Mixed, Mixed, ...Array<Mixed>]> // tslint:disable-next-line: deprecation
609609
extends TaggedUnionType<Tag, CS, TypeOf<CS[number]>, OutputOf<CS[number]>, unknown> {}
610610
```
611611

@@ -1413,16 +1413,19 @@ export class StringType {
14131413

14141414
Added in v1.0.0
14151415

1416-
# TaggedUnionType (class)
1416+
# ~~TaggedUnionType~~ (class)
14171417

14181418
**Signature**
14191419

14201420
```ts
14211421
export class TaggedUnionType<Tag, CS, A, O, I> {
14221422
constructor(
14231423
name: string,
1424+
// tslint:disable-next-line: deprecation
14241425
is: TaggedUnionType<Tag, CS, A, O, I>['is'],
1426+
// tslint:disable-next-line: deprecation
14251427
validate: TaggedUnionType<Tag, CS, A, O, I>['validate'],
1428+
// tslint:disable-next-line: deprecation
14261429
encode: TaggedUnionType<Tag, CS, A, O, I>['encode'],
14271430
codecs: CS,
14281431
readonly tag: Tag
@@ -2070,7 +2073,9 @@ export const success = <T>(value: T): Validation<T> => ...
20702073
20712074
Added in v1.0.0
20722075
2073-
# taggedUnion (function)
2076+
# ~~taggedUnion~~ (function)
2077+
2078+
Use `union` instead
20742079
20752080
**Signature**
20762081
@@ -2079,6 +2084,7 @@ export const taggedUnion = <Tag extends string, CS extends [Mixed, Mixed, ...Arr
20792084
tag: Tag,
20802085
codecs: CS,
20812086
name: string = getUnionName(codecs)
2087+
// tslint:disable-next-line: deprecation
20822088
): TaggedUnionC<Tag, CS> => ...
20832089
```
20842090

src/index.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,7 @@ export const union = <CS extends [Mixed, Mixed, ...Array<Mixed>]>(
10801080
}
10811081
return undefined
10821082
}
1083+
// tslint:disable-next-line: deprecation
10831084
return new TaggedUnionType(
10841085
name,
10851086
(u): u is TypeOf<CS[number]> => {
@@ -1460,6 +1461,7 @@ export const strict = <P extends Props>(props: P, name?: string): ExactC<TypeC<P
14601461

14611462
/**
14621463
* @since 1.3.0
1464+
* @deprecated
14631465
*/
14641466
export class TaggedUnionType<
14651467
Tag extends string,
@@ -1470,8 +1472,11 @@ export class TaggedUnionType<
14701472
> extends UnionType<CS, A, O, I> {
14711473
constructor(
14721474
name: string,
1475+
// tslint:disable-next-line: deprecation
14731476
is: TaggedUnionType<Tag, CS, A, O, I>['is'],
1477+
// tslint:disable-next-line: deprecation
14741478
validate: TaggedUnionType<Tag, CS, A, O, I>['validate'],
1479+
// tslint:disable-next-line: deprecation
14751480
encode: TaggedUnionType<Tag, CS, A, O, I>['encode'],
14761481
codecs: CS,
14771482
readonly tag: Tag
@@ -1482,23 +1487,30 @@ export class TaggedUnionType<
14821487

14831488
/**
14841489
* @since 1.5.3
1490+
* @deprecated
14851491
*/
1486-
export interface TaggedUnionC<Tag extends string, CS extends [Mixed, Mixed, ...Array<Mixed>]>
1492+
export interface TaggedUnionC<Tag extends string, CS extends [Mixed, Mixed, ...Array<Mixed>]> // tslint:disable-next-line: deprecation
14871493
extends TaggedUnionType<Tag, CS, TypeOf<CS[number]>, OutputOf<CS[number]>, unknown> {}
14881494

14891495
/**
1496+
* Use `union` instead
1497+
*
14901498
* @since 1.3.0
1499+
* @deprecated
14911500
*/
14921501
export const taggedUnion = <Tag extends string, CS extends [Mixed, Mixed, ...Array<Mixed>]>(
14931502
tag: Tag,
14941503
codecs: CS,
14951504
name: string = getUnionName(codecs)
1505+
// tslint:disable-next-line: deprecation
14961506
): TaggedUnionC<Tag, CS> => {
14971507
const U = union(codecs, name)
1508+
// tslint:disable-next-line: deprecation
14981509
if (U instanceof TaggedUnionType) {
14991510
return U
15001511
} else {
15011512
console.warn(`[io-ts] Cannot build a tagged union for ${name}, returning a de-optimized union`)
1513+
// tslint:disable-next-line: deprecation
15021514
return new TaggedUnionType(name, U.is, U.validate, U.encode, codecs, tag)
15031515
}
15041516
}

test/taggedUnion.ts

+14
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import * as assert from 'assert'
22
import * as t from '../src/index'
33
import { assertFailure, assertStrictEqual, assertSuccess, NumberFromString } from './helpers'
44

5+
// tslint:disable-next-line: deprecation
56
const OptionNumber = t.taggedUnion(
67
'type',
78
[t.type({ type: t.literal('None') }, 'None'), t.type({ type: t.literal('Some'), value: t.number }, 'Some')],
89
'OptionNumber'
910
)
1011

12+
// tslint:disable-next-line: deprecation
1113
const OptionNumberFromString = t.taggedUnion(
1214
'type',
1315
[t.type({ type: t.literal('None') }, 'None'), t.type({ type: t.literal('Some'), value: NumberFromString }, 'Some')],
@@ -17,6 +19,7 @@ const OptionNumberFromString = t.taggedUnion(
1719
describe('taggedUnion', () => {
1820
describe('name', () => {
1921
it('should assign a default name', () => {
22+
// tslint:disable-next-line: deprecation
2023
const OptionNumber = t.taggedUnion('type', [
2124
t.type({ type: t.literal('None') }),
2225
t.type({ type: t.literal('Some'), value: t.number })
@@ -25,6 +28,7 @@ describe('taggedUnion', () => {
2528
})
2629

2730
it('should accept a name', () => {
31+
// tslint:disable-next-line: deprecation
2832
const T = t.taggedUnion('type', OptionNumber.types, 'T')
2933
assert.strictEqual(T.name, 'T')
3034
})
@@ -56,6 +60,7 @@ describe('taggedUnion', () => {
5660
it('should handle intersections', () => {
5761
const A = t.intersection([t.type({ type: t.literal('A') }), t.partial({ a: t.string })], 'A')
5862
const B = t.type({ type: t.literal('B'), b: t.number }, 'B')
63+
// tslint:disable-next-line: deprecation
5964
const T = t.taggedUnion('type', [A, B], 'T')
6065
assertSuccess(T.decode({ type: 'A' }))
6166
assertSuccess(T.decode({ type: 'B', b: 1 }))
@@ -74,6 +79,7 @@ describe('taggedUnion', () => {
7479
forest: t.array(Self)
7580
})
7681
)
82+
// tslint:disable-next-line: deprecation
7783
const T = t.taggedUnion('type', [A, B], 'T')
7884
assertSuccess(T.decode({ type: 'A' }))
7985
assertSuccess(T.decode({ type: 'B', forest: [] }))
@@ -88,6 +94,7 @@ describe('taggedUnion', () => {
8894
const B = t.type({ type: t.literal('B'), b: t.string }, 'B')
8995
const C = t.type({ type: t.literal('C') }, 'C')
9096
const SubUnion = t.union([A, B], 'Subunion')
97+
// tslint:disable-next-line: deprecation
9198
const T = t.taggedUnion('type', [SubUnion, C], 'T')
9299
assertSuccess(T.decode({ type: 'A' }))
93100
assertSuccess(T.decode({ type: 'B', b: 'b' }))
@@ -99,7 +106,9 @@ describe('taggedUnion', () => {
99106
const A = t.type({ type: t.literal('A') })
100107
const B = t.type({ type: t.literal('B') })
101108
const C = t.type({ type: t.literal('C') })
109+
// tslint:disable-next-line: deprecation
102110
const SubTaggedUnion = t.taggedUnion('type', [A, B])
111+
// tslint:disable-next-line: deprecation
103112
const T = t.taggedUnion('type', [SubTaggedUnion, C])
104113
assertSuccess(T.decode({ type: 'A' }))
105114
assertSuccess(T.decode({ type: 'B' }))
@@ -112,6 +121,7 @@ describe('taggedUnion', () => {
112121
})
113122

114123
it('should support numeric tags', () => {
124+
// tslint:disable-next-line: deprecation
115125
const T = t.taggedUnion('type', [
116126
t.type({ type: t.literal(1), a: t.string }),
117127
t.type({ type: t.literal(2), b: t.number })
@@ -126,6 +136,7 @@ describe('taggedUnion', () => {
126136
})
127137

128138
it('should support boolean tags', () => {
139+
// tslint:disable-next-line: deprecation
129140
const T = t.taggedUnion('type', [
130141
t.type({ type: t.literal(true), a: t.string }),
131142
t.type({ type: t.literal(false), b: t.number })
@@ -140,6 +151,7 @@ describe('taggedUnion', () => {
140151
})
141152

142153
it('should support mixed string, numeric and boolean tags', () => {
154+
// tslint:disable-next-line: deprecation
143155
const T = t.taggedUnion(
144156
'type',
145157
[
@@ -181,6 +193,7 @@ describe('taggedUnion', () => {
181193
})
182194

183195
it('should handle one codec', () => {
196+
// tslint:disable-next-line: deprecation
184197
const T = t.taggedUnion('type', [t.type({ type: t.literal('A') })] as any)
185198
assertSuccess(T.decode({ type: 'A' }))
186199
assertFailure(T, null, ['Invalid value null supplied to : ({ type: "A" })'])
@@ -190,6 +203,7 @@ describe('taggedUnion', () => {
190203
const log: Array<string> = []
191204
const original = console.warn
192205
console.warn = (message: string) => log.push(message)
206+
// tslint:disable-next-line: deprecation
193207
t.taggedUnion('type', [t.type({ type: t.literal('a') }), t.type({ bad: t.literal('b') })])
194208
console.warn = original
195209
assert.deepStrictEqual(log, [

0 commit comments

Comments
 (0)