Skip to content

Commit 8c96359

Browse files
committed
Change the default tag field from "kind" to "type".
1 parent ea71b83 commit 8c96359

File tree

4 files changed

+39
-39
lines changed

4 files changed

+39
-39
lines changed

packages/match/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ Control-flow component for matching discriminated union (tagged union) members.
2828

2929
```tsx
3030
type MyUnion = {
31-
kind: "foo",
31+
type: "foo",
3232
foo: "foo-value",
3333
} | {
34-
kind: "bar",
34+
type: "bar",
3535
bar: "bar-value",
3636
}
3737

38-
const [value, setValue] = createSignal<MyUnion>({kind: "foo", foo: "foo-value"})
38+
const [value, setValue] = createSignal<MyUnion>({type: "foo", foo: "foo-value"})
3939

4040
<Match on={value()} case={{
4141
foo: v => <>{v().foo}</>,
@@ -45,18 +45,18 @@ const [value, setValue] = createSignal<MyUnion>({kind: "foo", foo: "foo-value"})
4545

4646
### Changing the tag key
4747

48-
The default tag key is `"kind"`, but it can be changed with the `tag` prop:
48+
The default tag key is `"type"`, but it can be changed with the `tag` prop:
4949

5050
```tsx
5151
type MyUnion = {
52-
type: "foo",
52+
kind: "foo",
5353
foo: "foo-value",
5454
} | {
55-
type: "bar",
55+
kind: "bar",
5656
bar: "bar-value",
5757
}
5858

59-
<Match on={value()} tag="type" case={{
59+
<Match on={value()} tag="kind" case={{
6060
foo: v => <>{v().foo}</>,
6161
bar: v => <>{v().bar}</>,
6262
}} />

packages/match/dev/index.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Component, createSignal } from "solid-js";
22
import { Match } from "../src/index.js"
33

4-
type AnimalDog = {kind: 'dog', breed: string};
5-
type AnimalCat = {kind: 'cat', lives: number};
6-
type AnimalBird = {kind: 'bird', canFly: boolean};
4+
type AnimalDog = {type: 'dog', breed: string};
5+
type AnimalCat = {type: 'cat', lives: number};
6+
type AnimalBird = {type: 'bird', canFly: boolean};
77

88
type Animal = AnimalDog | AnimalCat | AnimalBird;
99

@@ -42,9 +42,9 @@ const App: Component = () => {
4242

4343
const animals: (Animal | null)[] = [
4444
null,
45-
{ kind: 'dog', breed: 'Golden Retriever' },
46-
{ kind: 'cat', lives: 9 },
47-
{ kind: 'bird', canFly: true },
45+
{ type: 'dog', breed: 'Golden Retriever' },
46+
{ type: 'cat', lives: 9 },
47+
{ type: 'bird', canFly: true },
4848
];
4949

5050
return (

packages/match/src/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import { type Accessor, type JSX, createMemo } from "solid-js"
66
* @example
77
* ```tsx
88
* type MyUnion = {
9-
* kind: 'foo',
9+
* type: 'foo',
1010
* foo: 'foo-value',
1111
* } | {
12-
* kind: 'bar',
12+
* type: 'bar',
1313
* bar: 'bar-value',
1414
* }
1515
*
16-
* const [value, setValue] = createSignal<MyUnion>({kind: 'foo', foo: 'foo-value'})
16+
* const [value, setValue] = createSignal<MyUnion>({type: 'foo', foo: 'foo-value'})
1717
*
1818
* <Match on={value()} case={{
1919
* foo: v => <>{v().foo}</>,
@@ -32,10 +32,10 @@ export function Match<
3232
partial?: false,
3333
}): JSX.Element
3434
export function Match<
35-
T extends {kind: PropertyKey},
35+
T extends {type: PropertyKey},
3636
>(props: {
3737
on: T | null | undefined,
38-
case: {[K in T['kind']]: (v: Accessor<T & {[k in 'kind']: K}>) => JSX.Element},
38+
case: {[K in T['type']]: (v: Accessor<T & {[k in 'type']: K}>) => JSX.Element},
3939
fallback?: JSX.Element,
4040
partial?: false,
4141
}): JSX.Element
@@ -50,14 +50,14 @@ export function Match<
5050
partial: true,
5151
}): JSX.Element
5252
export function Match<
53-
T extends {kind: PropertyKey},
53+
T extends {type: PropertyKey},
5454
>(props: {
5555
on: T | null | undefined,
56-
case: {[K in T['kind']]?: (v: Accessor<T & {[k in 'kind']: K}>) => JSX.Element},
56+
case: {[K in T['type']]?: (v: Accessor<T & {[k in 'type']: K}>) => JSX.Element},
5757
fallback?: JSX.Element,
5858
partial: true,
5959
}): JSX.Element
6060
export function Match(props: any): any {
61-
const kind = createMemo(() => props.on?.[props.tag ?? 'kind'])
61+
const kind = createMemo(() => props.on?.[props.tag ?? 'type'])
6262
return createMemo(() => props.case[kind()]?.(() => props.on) ?? props.fallback)
6363
}

packages/match/test/index.test.tsx

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import * as s from "solid-js";
33
import { Match } from "../src/index.js";
44

55
v.describe("Match", () => {
6-
v.test("match on kind field", () => {
6+
v.test("match on type field", () => {
77

88
type MyUnion = {
9-
kind: 'foo',
9+
type: 'foo',
1010
foo: 'foo-value',
1111
} | {
12-
kind: 'bar',
12+
type: 'bar',
1313
bar: 'bar-value',
1414
}
1515

@@ -29,22 +29,22 @@ v.describe("Match", () => {
2929

3030
v.expect(data.result()).toEqual(undefined);
3131

32-
setValue({kind: 'foo', foo: 'foo-value'});
32+
setValue({type: 'foo', foo: 'foo-value'});
3333
v.expect(data.result()).toEqual(<>foo-value</>);
3434

35-
setValue({kind: 'bar', bar: 'bar-value'});
35+
setValue({type: 'bar', bar: 'bar-value'});
3636
v.expect(data.result()).toEqual(<>bar-value</>);
3737

3838
data.dispose();
3939
});
4040

41-
v.test("match on type field", () => {
41+
v.test("match on kind field", () => {
4242

4343
type MyUnion = {
44-
type: 'foo',
44+
kind: 'foo',
4545
foo: 'foo-value',
4646
} | {
47-
type: 'bar',
47+
kind: 'bar',
4848
bar: 'bar-value',
4949
}
5050

@@ -54,7 +54,7 @@ v.describe("Match", () => {
5454
return {
5555
dispose,
5656
result: s.children(() => <>
57-
<Match on={value()} tag='type' case={{
57+
<Match on={value()} tag='kind' case={{
5858
foo: v => <>{v().foo}</>,
5959
bar: v => <>{v().bar}</>,
6060
}} />
@@ -64,10 +64,10 @@ v.describe("Match", () => {
6464

6565
v.expect(data.result()).toEqual(undefined);
6666

67-
setValue({type: 'foo', foo: 'foo-value'});
67+
setValue({kind: 'foo', foo: 'foo-value'});
6868
v.expect(data.result()).toEqual(<>foo-value</>);
6969

70-
setValue({type: 'bar', bar: 'bar-value'});
70+
setValue({kind: 'bar', bar: 'bar-value'});
7171
v.expect(data.result()).toEqual(<>bar-value</>);
7272

7373
data.dispose();
@@ -76,10 +76,10 @@ v.describe("Match", () => {
7676
v.test("partial match", () => {
7777

7878
type MyUnion = {
79-
kind: 'foo',
79+
type: 'foo',
8080
foo: 'foo-value',
8181
} | {
82-
kind: 'bar',
82+
type: 'bar',
8383
bar: 'bar-value',
8484
}
8585

@@ -98,10 +98,10 @@ v.describe("Match", () => {
9898

9999
v.expect(data.result()).toEqual(undefined);
100100

101-
setValue({kind: 'foo', foo: 'foo-value'});
101+
setValue({type: 'foo', foo: 'foo-value'});
102102
v.expect(data.result()).toEqual(<>foo-value</>);
103103

104-
setValue({kind: 'bar', bar: 'bar-value'});
104+
setValue({type: 'bar', bar: 'bar-value'});
105105
v.expect(data.result()).toEqual(undefined);
106106

107107
data.dispose();
@@ -110,10 +110,10 @@ v.describe("Match", () => {
110110
v.test("fallback", () => {
111111

112112
type MyUnion = {
113-
kind: 'foo',
113+
type: 'foo',
114114
foo: 'foo-value',
115115
} | {
116-
kind: 'bar',
116+
type: 'bar',
117117
bar: 'bar-value',
118118
}
119119

@@ -133,7 +133,7 @@ v.describe("Match", () => {
133133

134134
v.expect(data.result()).toEqual(<>fallback</>);
135135

136-
setValue({kind: 'foo', foo: 'foo-value'});
136+
setValue({type: 'foo', foo: 'foo-value'});
137137
v.expect(data.result()).toEqual(<>foo-value</>);
138138

139139
setValue(undefined);

0 commit comments

Comments
 (0)