Skip to content

Commit

Permalink
feat(ts): Add types and TS documentation
Browse files Browse the repository at this point in the history
Add test

Add test
  • Loading branch information
rozzilla committed Apr 20, 2024
1 parent 752893f commit ea2f928
Show file tree
Hide file tree
Showing 5 changed files with 5,844 additions and 4 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,34 @@ setAll({
console.log(c(), d()); // 42 24
```

## TypeScript

You can call the `asyncforge` functions in a type-safe way:

```ts
// This will return the type { foo: string }
forge(() => ({ foo: "bar" }))()

const memoNum = memo<number>();

// This is okay for TypeScript, since you're passing a number
memoNum.set(123);

// This will not build
memoNum.set('wrong');

// The `result` var will be of type `number`
const result = memoNum()

const test = memo<string>();

// This is correct, since `test.key` is a `symbol`
setAll({ [test.key]: 42 });

// This will fail
setAll({ 'wrong': 42 });
```

## License

MIT
17 changes: 17 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type ForgeCallback<T, U> = {
(config: T): U;
name?: string;
};

export function start<T extends unknown>(config: T): void;
export function forge<T extends unknown, U extends unknown = unknown>(
fn: ForgeCallback<T, U>
): () => ReturnType<ForgeCallback<T, U>>;
export function memo<T extends unknown>(
name?: string
): {
(): T;
key: symbol;
set: (value: T) => void;
};
export function setAll(memos: Record<symbol, unknown>): void;
49 changes: 49 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { expectAssignable, expectError, expectType } from "tsd";
import { start, forge, memo, setAll } from ".";

// start
const config = 42;
expectType<void>(start<number>(config));
expectError<void>(start<string>(config));
expectType<void>(start<{ valid: boolean }>({ valid: true }));

const validStart = (_: unknown) => console.log("this is valid");
expectAssignable<typeof start>(validStart);

const invalidStart = (_: string) => "this is invalid";
expectError<typeof start>(invalidStart);

// forge
const forgeObject = forge(() => ({ foo: "bar" }));
expectType<{ foo: string }>(forgeObject());
expectError<{ foo: boolean }>(forgeObject());

const forgeString = forge(() => "");
expectType<string>(forgeString());
expectError<boolean>(forgeString());

type TestData = { baz: string; foo: string };
const getFoo = forge<TestData>((config) => {
return {
data: config.baz,
value: config.foo,
};
});
expectType<unknown>(getFoo());

const getFooString = forge<TestData, string>((config) => {
return config.baz + config.foo;
});
expectType<string>(getFooString());

// memo
const memoNum = memo<number>();
expectType<symbol>(memoNum.key);
expectType<void>(memoNum.set(123));
expectType<number>(memoNum());
expectError<void>(memoNum.set("wrong"));

// setAll
const test = memo<string>();
expectType<void>(setAll({ [test.key]: 42 }));
expectError<void>(setAll({ wrong: 42 }));
Loading

0 comments on commit ea2f928

Please sign in to comment.