Change return type based on value of parameters

I'm trying to change the return type of a function based on the value of the arguments passed to the function itself. I'm struggling to find the right documentation for it, could anyone point me in the right direction Here's an example of what I want:
function doSomething({user, includePfp}){
if(includePfp) return {...user, pfp: 'wow'}
return user
}

doSomething({user}) // returns type of User
doSomething({user, includePfp: true}) // returns type of User & {pfp: string}
function doSomething({user, includePfp}){
if(includePfp) return {...user, pfp: 'wow'}
return user
}

doSomething({user}) // returns type of User
doSomething({user, includePfp: true}) // returns type of User & {pfp: string}
Solution:
Here is the final solution in case anyone is curious https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgUygYwEwAYCCAhASQBo4BXAZ2TwEMAbagOzWRIuQGEIGYpq0YASsmoATVpU7de-IaPJwAvnABmUCCDgAiAO7UA5iGCaA3AFgAUBZgBPMMjg1KAUQHtsABWq8Q8gLyILODgYCABrZAYcEREoZHJyAC44AAMsAA8AEgRyHmAGPQVkwLg0AAtqPIIRJIZSEAAjVDNzIJBkGFFqDoB+JPqICFphBmagtihelPSsnKg8guTRlAZqeqGRSf7B4aXdGDLNgaHGJeowMDUAN2RD7ZPi8jsGEVRJ1Mzs3PzCuAAfMmeyGUeWQImaCmaVls9hw5yuyBcbiwnm85AAPAAVRz2ZBpGARETybGIjxeag+AB8cH8CAeTxeUCSWOolAA2pozhcINdNABdFB4gnyZR0Sh-AEvYEMUHi2q0WjFboSoEgkTFJLvGZfBanOHc5A4EAQUjcJnY9mc+F8gX457C0X2f4myWq2WkeWK5VS0HquD1YB6PIwJa4vgwWFcy50SbMtkcvU8-m422ElQO8XOlXSkRuj0tOBKzPetX5vpHHYWCEWCwAehrwWhcAAqs5XKTUdSHCyEW3kWSfHAAGRwCPwkl91Fo4m9lHk8gU5o2OzN1tI2cD-zTtf98iQ8xL+zjgCy7U6HU7tPzKzaSVm8yW5GsDUGt+1SxeaFAopqdUaUCWIQdLQADKpDnLQ1h9AGQbgnuB4rj2SJCOQ7rwDSxRtB0IhdNQkzHqe2EdEs9R0IwzCbNB3CwdW+6NuOyGoSeWE4Z27jAGgoRoi2iHYAxtAwCQmiYWe1CaAuULLvRcSoTQ9BMPY-hsRxXGrrx0n8YJJFycwYlwXRvZ8TAmLrvIyZCgh44mVS6H5sJhHUEyJnsnZOHWmZdpprQYpOoCxa5gq+ZKtK1xQL6+HMeePkutmxGkfJjk7uy4xuYKHkil5jpeq6-xygFQRBcgIW+v6gbcBmvmqqc8oQLo8VwBiTmaI8BKoClKb2hl5XRTKOXunlBZwMFqC+g1iWaMlSapam6XeVl2b+Z6Q2haWfqUfAUVZqC1GWOYuKQLAKgmvwwBcGQqlYMZO42uZ3GWTuFIABSXq0BEsf4PCkCwxTjJ2RaqkQxRlBUDBVAD+YhOEkTRLE8Rg0EESrOsnYfV9+bNYCUC-RV2Zw3AexlJ2M2o0Elr6oTDpgwoCTFKNqIAJRJFJKH8ZdqJUpeNE0UEsQwKQUAMAE+YvRFDmC0E4uDeSyBJAA5BicQwDLuMS4+z60LL8s5ErxQSx+X5eUkACMAAcyvi4BdCgeBkEOAGBDcA9htYFgdM64oZtaWR0u23o9swI7Ltm3QtA1V7SR4HbDtO3TuNKCycBM6hrNzuJ5gKDRaBcDkiBwC5HQkMHofySQnvyYov3nU9gPlJU1RwIbuMQxEUQxHEiRwDL6RYNrad03AdZwCIEBxAwMvwHkyioINhVTweGdZ-ASB56Ly8YAX1W1cwSSF5vyBr36cVbwf2l7+X-hsOOVf5kDtdG43YTN9Dbey13PdjJQjJzaClN9wPQ9xHACeU8loNjsBYIAA...
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
Jump to solution
1 Reply
Solution
kinsyu
kinsyu2y ago
Here is the final solution in case anyone is curious https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgUygYwEwAYCCAhASQBo4BXAZ2TwEMAbagOzWRIuQGEIGYpq0YASsmoATVpU7de-IaPJwAvnABmUCCDgAiAO7UA5iGCaA3AFgAUBZgBPMMjg1KAUQHtsAB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TFKNqIAJRJFJKH8ZdqJUpeNE0UEsQwKQUAMAE+YvRFDmC0E4uDeSyBJAA5BicQwDLuMS4+z60LL8s5ErxQSx+X5eUkACMAAcyvi4BdCgeBkEOAGBDcA9htYFgdM64oZtaWR0u23o9swI7Ltm3QtA1V7SR4HbDtO3TuNKCycBM6hrNzuJ5gKDRaBcDkiBwC5HQkMHofySQnvyYov3nU9gPlJU1RwIbuMQxEUQxHEiRwDL6RYNrad03AdZwCIEBxAwMvwHkyioINhVTweGdZ-ASB56Ly8YAX1W1cwSSF5vyBr36cVbwf2l7+X-hsOOVf5kDtdG43YTN9Dbey13PdjJQjJzaClN9wPQ9xHACeU8loNjsBYIAA
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
Want results from more Discord servers?
Add your server