Skip to content

Commit

Permalink
use overloads for send, closes #65 (#68)
Browse files Browse the repository at this point in the history
as a workaround for typescript bug
  • Loading branch information
devanshj committed Jul 28, 2021
1 parent 3441a45 commit a10dfcc
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 33 deletions.
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,9 @@ export namespace Machine {
}

export type Send<D> =
(sendable: Sendable<D>) => void
{ (sendable: U.Exclude<Sendable<D>, A.String>): void
, (sendable: U.Extract<Sendable<D>, A.String>): void
}

type SendImpl = (send: Sendable.Impl) => void
export namespace Send {
Expand Down
106 changes: 74 additions & 32 deletions test/types.twoslash-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -818,12 +818,15 @@ describe("Machine.Definition", () => {

A.test(A.areEqual<
typeof effectParameter.send,
(sendable:
| "Y"
| { type: "X", foo: number }
| { type: "Y", bar?: number }
| { type: "Z", baz: string }
) => void
{ ( sendable:
| { type: "X", foo: number }
| { type: "Y", bar?: number }
| { type: "Z", baz: string }
): void
, ( sendable:
| "Y"
): void
}
>())

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

A.test(A.areEqual<
typeof send,
(sendable:
| "Y"
| { type: "X", foo: number }
| { type: "Y", bar?: number }
| { type: "Z", baz: string }
) => void
{ ( sendable:
| { type: "X", foo: number }
| { type: "Y", bar?: number }
| { type: "Z", baz: string }
): void
, ( sendable:
| "Y"
): void
}
>())

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

A.test(A.areEqual<
typeof cleanupParameter.send,
(sendable:
| "Y"
| { type: "X", foo: number }
| { type: "Y", bar?: number }
| { type: "Z", baz: string }
) => void
{ ( sendable:
| { type: "X", foo: number }
| { type: "Y", bar?: number }
| { type: "Z", baz: string }
): void
, ( sendable:
| "Y"
): void
}
>())

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

A.test(A.areEqual<
typeof send,
(sendable:
| "Y"
| { type: "X", foo: number }
| { type: "Y", bar?: number }
| { type: "Z", baz: string }
) => void
{ ( sendable:
| { type: "X", foo: number }
| { type: "Y", bar?: number }
| { type: "Z", baz: string }
): void
, ( sendable:
| "Y"
): void
}
>())
}
}
Expand Down Expand Up @@ -1164,13 +1176,16 @@ describe("UseStateMachine", () => {
describe("Machine.Send", () => {
A.test(A.areEqual<
typeof send,
(sendable:
| "Y"
| "Z"
| { type: "X", foo: number }
| { type: "Y", bar?: number }
| { type: "Z" }
) => void
{ ( sendable:
| { type: "X", foo: number }
| { type: "Y", bar?: number }
| { type: "Z" }
): void
, ( sendable:
| "Y"
| "Z"
): void
}
>())
})
})
Expand Down Expand Up @@ -1212,7 +1227,9 @@ describe("Machine.Definition.FromTypeParamter", () => {

A.test(A.areEqual<
typeof send,
(sendable: "TOGGLE" | { type: "TOGGLE" }) => void
{ (sendable: { type: "TOGGLE" }): void
, (sendable: "TOGGLE"): void
}
>())
})

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

A.test(A.areEqual<typeof state.nextEvents, "X"[]>())
})

describe("workaround for #65", () => {
let [_, send] = useStateMachine({
schema: {
events: {
A: t<{ value: string }>()
}
},
initial: "a",
states: {
a: {
on: {
B: "a"
}
}
}
})

A.test(A.areEqual<
typeof send,
{ (sendable: { type: "A", value: string } | { type: "B" }): void
, (sendable: "B"): void
}
>())
})

0 comments on commit a10dfcc

Please sign in to comment.