Skip to content

Instantly share code, notes, and snippets.

@og24715
Last active July 11, 2023 07:30
Show Gist options
  • Save og24715/b4c10bd3376727d35bf5ac5609921a8d to your computer and use it in GitHub Desktop.
Save og24715/b4c10bd3376727d35bf5ac5609921a8d to your computer and use it in GitHub Desktop.
interface Endpoint<Parameter, Response> {
parameter: Parameter,
response: Response,
}
/**
* @link https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgKIgCYAcD2owA8ACnFHALYSRQA0yAShAM64hMQB8yA3gLABQyIciykKVaAC5kJMpWo0Bw5FGat20xixxsIi-gF8BAjBAQAbUigtwmTZAEEiASQDC54BHAF02POHsIAA9ITHtuZABtBBxycjhMaSYwKFAAcwBdaV9cfAIAVxAAaxAcAHcQOkKS8pAuAy4+QREcZIIAFWRg0Ix7VBCyBEIiiABPHBg0TFyAumTUkDSODgAKGLiEjGl2uhwsMGAdAH5s6f8wJkj2jMiAclE5CShbjIBKU798S+u71W1dF7IARGfgCGJsMDIOBYYDIAC8yBAEDKjhc7k83iaynYmAA+ggABZwMAfGaEebpOgAIxwOHMEASHH0yjSVFx+SwGGJEFJ5wIEUQMUKYFxwC2iPy5Cp0GQBjoERpOCK8SgRWk1VKFUiGVlHGBq1exn40OAADpcMkVrccRh8USwLdXgBuME6ZI8ZCK5WkIqy+FQmHm1pgK2skUcrmQW7yqEIIXgUXigCMssNxsDFpDt1M9MgduJjpd-CAA
**/
declare class APIClient<Endpoints extends { [command: string]: Endpoint<unknown, unknown> }> {
post<T extends Extract<keyof Endpoints, string>>(command: T, option?: Endpoints[T]['parameter']): Endpoints[T]['response']
}
const api = new APIClient<{
send_chat: Endpoint<string, boolean>,
get_update: Endpoint<{ account_id: number }, { bookmark: unknown[] }>
}>()
api.post('send_chat');
const { bookmark } = api.post('get_update', { account_id: 1 })
api.post('delete_chat');
/**
* @link https://www.typescriptlang.org/play?noUncheckedIndexedAccess=true&noPropertyAccessFromIndexSignature=true#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXwDcoItgoMQAeASQBF4QAPC1YAZ3jYxi1QHMAfAApSALnh0ANPDhsADnjYhxaANaocAd1QBKcVDZKYGDrIWol8LBwDe8ANqkr+OgF1xdtsjBgQh8dzICAC+wfAAsABQUaCQsAhgihgyfuZKKqjqWqgA3FEx4NBw8IkWyWKc3Lx8edGRRCRkFCLA0maKIDpR7RYgjsCuAHRePn5s+XUNpOQgQgDkAExzbakdXZE9SvaLc0MjvoZAA
**/
declare function validate<ID extends string>(id: ID, response: unknown): asserts response is { [id in ID]: { success: true }}
declare const response: unknown;
declare const id: string;
validate(id, response)
response[id].success
validate('2', response)
response['2'].success
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment