Created
September 6, 2019 21:40
-
-
Save javcasas/c343244246781c48ff67c1053e29fe77 to your computer and use it in GitHub Desktop.
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
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