Skip to content

Commit

Permalink
editor config, trying to infer error types
Browse files Browse the repository at this point in the history
  • Loading branch information
Inalegwu committed Oct 7, 2024
1 parent c45c430 commit ad8ba43
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
17 changes: 17 additions & 0 deletions .helix/languages.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[[language]]
name = "typescript"
language-id = "typescript"
scope = "source.ts"
injection-regex = "^(ts|typescript)$"
file-types = ["ts"]
shebangs = ["deno"]
roots = ["deno.json", "deno.jsonc", "package.json"]
auto-format = true
language-servers = ["deno-lsp"]

[language-server.deno-lsp]
command = "deno"
args = ["lsp"]

[language-server.deno-lsp.config.deno]
enable = true
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import { err, ok, type Result, ResultAsync } from "npm:[email protected]";
export type AnyFunction = (...args: never[]) => unknown;

/**
*
* Create a new function that returns a Result type
* Result<A,E>, where A is your return value and E is
* the possible Error
*
*
* In case of async functions, A ResultAsync<A,E> is returned
* when an `await` keyword isn't used otherwise a Result<A,E>
* of the resolved promise is returned
*
*
* @param func The function being wrapped
* @returns A new function that returns a Result Type (Result<A,E>)
*/
Expand All @@ -39,5 +38,7 @@ export function makeSafeFunction<F extends AnyFunction>(
} catch (error) {
return err(error instanceof Error ? error : new Error(String(error)));
}
// deno-lint-ignore no-explicit-any
}) as any;
// ^ aids type inference for some reason
}
27 changes: 18 additions & 9 deletions src/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { makeSafeFunction } from "./index.ts";

class NumberError extends Error {
constructor(message: string) {
super();
this.message = message;
}
}

const unsafeDivide = (a: number, b: number) => {
if (b === 0) throw new Error("Divide by zero error caught");
if (b === 0) throw new NumberError("Divide by zero error caught");

return a / b;
};
Expand All @@ -15,20 +22,22 @@ const unsafeAsyncFunction = async (url: string) => {
const safeAsyncFunction = makeSafeFunction(unsafeAsyncFunction);
const safeDivide = makeSafeFunction(unsafeDivide);

const result = await safeAsyncFunction("https://google.com").unwrapOr(
new Response("hello world",{
status:200,
statusText:"Success",
headers:{
"content-type":"application/json",
}
const result = await safeAsyncFunction(
"https://ergast.com/api/f1/2024/constructorStandings.json",
).unwrapOr(
new Response("hello world", {
status: 200,
statusText: "Success",
headers: {
"content-type": "application/json",
},
}),
);

const divideResult = safeDivide(1, 2).unwrapOr(0);
const divideByZeroResult = safeDivide(2, 0).unwrapOr(-1);

console.log(result);
console.log(await result.json());
console.log(divideResult);

if (divideByZeroResult === -1) {
Expand Down

0 comments on commit ad8ba43

Please sign in to comment.