Skip to content

Commit a10dfcc

Browse files
authored
use overloads for send, closes #65 (#68)
as a workaround for typescript bug
1 parent 3441a45 commit a10dfcc

File tree

2 files changed

+77
-33
lines changed

2 files changed

+77
-33
lines changed

src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,9 @@ export namespace Machine {
368368
}
369369

370370
export type Send<D> =
371-
(sendable: Sendable<D>) => void
371+
{ (sendable: U.Exclude<Sendable<D>, A.String>): void
372+
, (sendable: U.Extract<Sendable<D>, A.String>): void
373+
}
372374

373375
type SendImpl = (send: Sendable.Impl) => void
374376
export namespace Send {

test/types.twoslash-test.ts

Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -818,12 +818,15 @@ describe("Machine.Definition", () => {
818818

819819
A.test(A.areEqual<
820820
typeof effectParameter.send,
821-
(sendable:
822-
| "Y"
823-
| { type: "X", foo: number }
824-
| { type: "Y", bar?: number }
825-
| { type: "Z", baz: string }
826-
) => void
821+
{ ( sendable:
822+
| { type: "X", foo: number }
823+
| { type: "Y", bar?: number }
824+
| { type: "Z", baz: string }
825+
): void
826+
, ( sendable:
827+
| "Y"
828+
): void
829+
}
827830
>())
828831

829832
A.test(A.areEqual<
@@ -841,12 +844,15 @@ describe("Machine.Definition", () => {
841844

842845
A.test(A.areEqual<
843846
typeof send,
844-
(sendable:
845-
| "Y"
846-
| { type: "X", foo: number }
847-
| { type: "Y", bar?: number }
848-
| { type: "Z", baz: string }
849-
) => void
847+
{ ( sendable:
848+
| { type: "X", foo: number }
849+
| { type: "Y", bar?: number }
850+
| { type: "Z", baz: string }
851+
): void
852+
, ( sendable:
853+
| "Y"
854+
): void
855+
}
850856
>())
851857

852858
return (cleanupParameter) => {
@@ -861,12 +867,15 @@ describe("Machine.Definition", () => {
861867

862868
A.test(A.areEqual<
863869
typeof cleanupParameter.send,
864-
(sendable:
865-
| "Y"
866-
| { type: "X", foo: number }
867-
| { type: "Y", bar?: number }
868-
| { type: "Z", baz: string }
869-
) => void
870+
{ ( sendable:
871+
| { type: "X", foo: number }
872+
| { type: "Y", bar?: number }
873+
| { type: "Z", baz: string }
874+
): void
875+
, ( sendable:
876+
| "Y"
877+
): void
878+
}
870879
>())
871880

872881
A.test(A.areEqual<
@@ -884,12 +893,15 @@ describe("Machine.Definition", () => {
884893

885894
A.test(A.areEqual<
886895
typeof send,
887-
(sendable:
888-
| "Y"
889-
| { type: "X", foo: number }
890-
| { type: "Y", bar?: number }
891-
| { type: "Z", baz: string }
892-
) => void
896+
{ ( sendable:
897+
| { type: "X", foo: number }
898+
| { type: "Y", bar?: number }
899+
| { type: "Z", baz: string }
900+
): void
901+
, ( sendable:
902+
| "Y"
903+
): void
904+
}
893905
>())
894906
}
895907
}
@@ -1164,13 +1176,16 @@ describe("UseStateMachine", () => {
11641176
describe("Machine.Send", () => {
11651177
A.test(A.areEqual<
11661178
typeof send,
1167-
(sendable:
1168-
| "Y"
1169-
| "Z"
1170-
| { type: "X", foo: number }
1171-
| { type: "Y", bar?: number }
1172-
| { type: "Z" }
1173-
) => void
1179+
{ ( sendable:
1180+
| { type: "X", foo: number }
1181+
| { type: "Y", bar?: number }
1182+
| { type: "Z" }
1183+
): void
1184+
, ( sendable:
1185+
| "Y"
1186+
| "Z"
1187+
): void
1188+
}
11741189
>())
11751190
})
11761191
})
@@ -1212,7 +1227,9 @@ describe("Machine.Definition.FromTypeParamter", () => {
12121227

12131228
A.test(A.areEqual<
12141229
typeof send,
1215-
(sendable: "TOGGLE" | { type: "TOGGLE" }) => void
1230+
{ (sendable: { type: "TOGGLE" }): void
1231+
, (sendable: "TOGGLE"): void
1232+
}
12161233
>())
12171234
})
12181235

@@ -1231,3 +1248,28 @@ describe("fix(Machine.State['nextEvents']): only normalize don't widen", () => {
12311248

12321249
A.test(A.areEqual<typeof state.nextEvents, "X"[]>())
12331250
})
1251+
1252+
describe("workaround for #65", () => {
1253+
let [_, send] = useStateMachine({
1254+
schema: {
1255+
events: {
1256+
A: t<{ value: string }>()
1257+
}
1258+
},
1259+
initial: "a",
1260+
states: {
1261+
a: {
1262+
on: {
1263+
B: "a"
1264+
}
1265+
}
1266+
}
1267+
})
1268+
1269+
A.test(A.areEqual<
1270+
typeof send,
1271+
{ (sendable: { type: "A", value: string } | { type: "B" }): void
1272+
, (sendable: "B"): void
1273+
}
1274+
>())
1275+
})

0 commit comments

Comments
 (0)