-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Discriminate return type based on property in input object #31335
Labels
Duplicate
An existing issue was already created
Comments
Duplicate of #22735. Here is an example of why this wont work: const search = Search();
const args: Args = { select: true };
const empty: {} = args;
const oops: boolean = search.findIt(empty); // evaluates to 1 at run time. |
Thanks a lot for the answer! Looks like I'll have to go with an explicit return type annotation for now. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TypeScript Version: 3.5.0-dev.20190509
Search Terms:
discriminate return type, keyof, extends
Expected behavior:
I want to discriminate the return type to "if a property in an object exists, return something different".
These are the 2 approaches I try, both don't work.
'select' extends keyof T
Playground LinkError:
T extends ArgsWithSelect
Playground LinkError:
I'm running on
[email protected]
.Testing both interface definitions on a type level - they work perfectly. The only problem here is that TypeScript is not able to correctly infer the types from the ternary expression in the actual implementation.
In other words, I'm able to express this on a TypeSystem level, but I'm not able to provide an implementation that the compiler accepts.
Playground Link:
First Playground
Second Playground
Related Issues:
#28597
The text was updated successfully, but these errors were encountered: