Skip to content

Instantly share code, notes, and snippets.

@javcasas
Created September 6, 2019 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javcasas/c343244246781c48ff67c1053e29fe77 to your computer and use it in GitHub Desktop.
Save javcasas/c343244246781c48ff67c1053e29fe77 to your computer and use it in GitHub Desktop.
type APIWithExtra = {
hasExtra: true;
field: string;
optionalField: string;
}
type APINoExtra = {
hasExtra: false;
field: string;
}
type API<T extends boolean> =
T extends true ? APIWithExtra : APINoExtra;
function getAPI(addExtra: boolean): API<typeof addExtra> {
if (addExtra) {
const result : API<typeof addExtra> = {
hasExtra: true,
field: "bdbf",
optionalField: "sda"
};
return result;
} else {
const result : API<typeof addExtra> = {
hasExtra: false,
field: "bdbf"
};
return result
}
}
const a : boolean & number = true;
const x : (typeof a) extends (true | false) ? number : never = 5;
function getAPI2<T extends true | false>(addExtra: T): API<typeof addExtra> {
if (addExtra) {
const result : API<typeof addExtra> = {
hasExtra: true,
field: "bdbf",
optionalField: "sda"
};
return result;
} else {
const result : API<typeof addExtra> = {
hasExtra: false,
field: "bdbf"
};
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment