Created
March 28, 2022 02:12
-
-
Save devjmetivier/b39fb832aabaac5ce59291dff75999e8 to your computer and use it in GitHub Desktop.
AsyncReturnType: TypeScript utility type
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
// https://www.typescriptlang.org/play?#code/C4TwDgpgBAggziAdgYwEoWAVwE6ICrgQA8eUEAHsBIgCZxQAUAdCwIbYDmcAXFK4iACUUALwA+PgIkiAUFCgB6BVACWAMyikAtq2DIAFhHrB9K+nBUdEunNH40o2DDkT1WUAArYA9lrMQAGjJKbFZkYCgAVUYTaFBIKG8NWMcjbwAbADcIBzAfPzgIYXsoTEKoE105TWCqWnpmNk4eSSFRCS9ffyIVRDUIbCixavkAfiiRqF4lROwWJnl1Gp09Q2NTc0trLCdJBycd11aTXo4ydMKgimBQ8IrDVMOoTNZ0zGhokrK4-Sr5eVI12odEY83YXF4-Da4lUfQGE3+UHGkUm02U3jmLFUGgg2WwIBOiDOHG8Rgq3ighnS6SCBxcUAABlCGZN5FCANwyLmsBAoKBqTAoYAqbyIPi85AAMUFyAYwgA3pM6bgKth3pyAL5cgVCkVixCi+BIKUyuVQRWI5Vim7qmRamTxaAAOUNEvQhzgnQKEEl+SNfJEUHdLgIkCIjqS4uN0pQYk5jqgLsQ-rQzlwcFDPvySZToiDafwhHDhEjBuTEpjyDjXITKeD6czvt8ucDdYLmeLkEjPOjMurtbdBYzhCbWhzErzbcOHYjGjLKcrcaAA | |
type AsyncReturnType<T extends (...args: any) => any> = | |
// if T matches this signature and returns a Promise, extract U (the type of the resolved promise) and use that | |
T extends (...args: any) => Promise<infer U> | |
? U | |
: // or... if T matches this signature and returns anything else, extract the return value U and use that | |
T extends (...args: any) => infer U | |
? U | |
: // or... if everything goes to hell, return `any` | |
any; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment