Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ts): Add types and TS documentation #2

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,51 @@ setAll({
console.log(c(), d()); // 42 24
```

## TypeScript

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

```ts
// You can define the `AsyncForgeConfig` interface so that `start` and `forge` can use it (TS module augmentation)
declare module "asyncforge" {
interface AsyncForgeConfig {
foo: string;
baz: number;
}
}

// This is correct
start({ foo: "bar", baz: 42 })

// TypeScript will complain, since it's not following the definition of AsyncForgeConfig
start({ wrong: true })

// Valid
forge(({ baz: data, foo: value }) => ({ data, value }));

// Invalid
forge(({ invalid }) => ({ invalid }));

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
23 changes: 23 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
declare namespace asyncforge {
interface AsyncForgeConfig {}

type ForgeCallback<T> = {
(config: AsyncForgeConfig): T;
name?: string;
};

export function start(config: AsyncForgeConfig): void;
export function forge<T extends unknown>(
fn: ForgeCallback<T>
): () => ReturnType<ForgeCallback<T>>;
export function memo<T extends unknown>(
name?: string
): {
(): T;
key: symbol;
set: (value: T) => void;
};
export function setAll(memos: Record<symbol, unknown>): void;
}

export = asyncforge
46 changes: 46 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { expectError, expectType } from "tsd";
import { start, forge, memo, setAll } from ".";

declare module "." {
interface AsyncForgeConfig {
foo: string;
baz: number;
}
}

// start
expectType<void>(start({ foo: "bar", baz: 42 }));
expectType<void>(start({ baz: 24, foo: "xyz" }));
expectError<void>(start({}));
expectError<void>(start({ foo: false, baz: 42 }));

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

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

const remapForge = forge(({ baz: data, foo: value }) => ({ data, value }));
expectType<{ data: number; value: string }>(remapForge());
expectType<() => void>(forge((config) => console.log(config.baz)));
expectError<() => void>(forge((config) => config.invalid));

const getFooString = forge((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
Loading