Skip to content

Instantly share code, notes, and snippets.

@jwulf
Last active February 15, 2021 06:13
Show Gist options
  • Save jwulf/9a0c17220816a7b4a35e58ab5067e962 to your computer and use it in GitHub Desktop.
Save jwulf/9a0c17220816a7b4a35e58ab5067e962 to your computer and use it in GitHub Desktop.
A minimal attempt to strongly type a query response
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