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

Fix types #57

Merged
merged 4 commits into from
Oct 19, 2023
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
5 changes: 5 additions & 0 deletions .changeset/warm-ducks-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ssecd/jkn': patch
---

fix types
22 changes: 15 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
import { PCare } from './pcare/index.js';
import { VClaim } from './vclaim/index.js';

type JKNResponseType<T extends object, K extends keyof T> = NonNullable<
'response' extends keyof Awaited<ReturnType<T[K]>> ? Awaited<ReturnType<T[K]>>['response'] : never
>;

export default class JKN extends Fetcher {
private readonly cache = new CachedApi(this);

Expand Down Expand Up @@ -39,9 +35,17 @@
}
}

type JKNResponseType<T extends object, K extends keyof T> = NonNullable<
// @ts-expect-error T[K] will always a method of class T
'response' extends keyof Awaited<ReturnType<T[K]>> ? Awaited<ReturnType<T[K]>>['response'] : never
>;

// cannot use built-in Parameters type helper with class type that return from method eg. VClaim[T][K]
type MethodParameters<T> = T extends (...args: infer P) => unknown ? P : never;

Check failure on line 44 in src/index.ts

View workflow job for this annotation

GitHub Actions / release

Unused '@ts-expect-error' directive.

export type AntreanResponse<K extends keyof Antrean> = JKNResponseType<Antrean, K>;

export type AntreanParams<K extends keyof Antrean> = Parameters<Antrean[K]>;
export type AntreanParams<K extends keyof Antrean> = MethodParameters<Antrean[K]>;

export type VClaimResponse<
T extends keyof VClaim, //
Expand All @@ -51,7 +55,7 @@
export type VClaimParams<
T extends keyof VClaim, //
K extends keyof VClaim[T]
> = Parameters<VClaim[T][K]>;
> = MethodParameters<VClaim[T][K]>;

export type ApotekResponse<
T extends keyof Apotek, //
Expand All @@ -61,4 +65,8 @@
export type ApotekParams<
T extends keyof Apotek, //
K extends keyof Apotek[T]
> = Parameters<Apotek[T][K]>;
> = MethodParameters<Apotek[T][K]>;

export type ICareResponse<K extends keyof ICare> = JKNResponseType<ICare, K>;

export type ICareParams<K extends keyof ICare> = MethodParameters<ICare[K]>;
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"resolveJsonModule": true,
"moduleResolution": "NodeNext",
"strict": true,
"module": "esnext",
"module": "NodeNext",
"target": "esnext",
"lib": ["esnext", "DOM"],
"outDir": "dist",
Expand Down
Loading