Skip to content

Instantly share code, notes, and snippets.

@mfbx9da4
Created November 30, 2018 12:58
Show Gist options
  • Save mfbx9da4/52644ce5e8f7c1894516d3da8f09e754 to your computer and use it in GitHub Desktop.
Save mfbx9da4/52644ce5e8f7c1894516d3da8f09e754 to your computer and use it in GitHub Desktop.
type A = {
company_name: string
}
type B = {
person_name: string
}
enum CBURL {
GET_COMPANY = "/companies",
GET_PEOPLE = "/people"
}
interface CB_RESPONSES {
[CBURL.GET_COMPANY]: A
[CBURL.GET_PEOPLE]: B
}
interface CB_REQUEST {
[CBURL.GET_COMPANY]: CB_URL_UUID_REQUEST<CBURL.GET_COMPANY>
[CBURL.GET_PEOPLE]: CB_URL_REQUEST<CBURL.GET_PEOPLE>
}
interface CB_URL_REQUEST<U extends CBURL> {
url: U,
params: {
page?: number
}
}
interface CB_URL_UUID_REQUEST<U extends CBURL> extends CB_URL_REQUEST<U> {
uuid: string
}
function fetch<U extends CBURL, R extends CB_URL_REQUEST<U>>(
request: R extends CB_URL_UUID_REQUEST<infer U> ? CB_URL_UUID_REQUEST<U> : CB_URL_REQUEST<U>
): CB_RESPONSES[U] {
return {}
}
let companyTest: A = fetch({
url: CBURL.GET_COMPANY,
params: {}
})
let personTest: B = fetch({
url: CBURL.GET_PEOPLE,
params: {}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment