Last active
February 15, 2021 06:13
-
-
Save jwulf/9a0c17220816a7b4a35e58ab5067e962 to your computer and use it in GitHub Desktop.
A minimal attempt to strongly type a query response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { dispatch, spawn, query, Ref, start } from "nact"; | |
type Result = { success: boolean }; | |
type DoLoadMessage = { | |
type: "LOAD"; | |
filename: string; | |
sender: Ref<Result>; | |
}; | |
const system = start(); | |
const fileLoader = spawn( | |
system, | |
(msg: DoLoadMessage, _) => { | |
if (msg.type === "LOAD") { | |
dispatch(msg.sender, { success: true }); | |
} | |
}, | |
"Fileloader" | |
); | |
query(fileLoader, (sender) => ({ sender, filename: "test.txt" }), 250).then( | |
(result) => { | |
result; // result is type any | |
} | |
// Update - explicitly typing the tempRef sender flows through | |
query( | |
fileLoader, | |
(sender: Ref<Result>) => ({ sender, filename: "test.txt" }), | |
250 | |
).then((result) => { | |
result; // result is type Result | |
}); | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment