-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ts): Add types and TS documentation
Add test Add test
- Loading branch information
Showing
5 changed files
with
5,844 additions
and
4 deletions.
There are no files selected for viewing
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
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
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; |
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
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 })); |
Oops, something went wrong.