-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Generic key name for object in callback function works only partially #40851
Comments
This is a correct error, useObjectWithSingleNamedKey<"foobar" | "bazbaz">('foobar', 'test', obj => {
// Crashes
console.log(obj.bazbaz.toUpperCase());
}); |
@RyanCavanaugh Hm, ok, that makes sense I guess. But, then, how do I do this correctly? Hopefully my intent is clear, but how can I make that intent clear to Typescript here? |
There isn't a way to express this (at least not without some trickery someone more clever than me would need to come up with). If you pinky promise to yourself not to call this with a union argument, I would just add a type assertion and call it a day. |
Well, that's annoying. 😕 Guess this is a feature request then, rather than a bug report. Because I really feel like there should be some way to express my intent. In my specific case, it shouldn't be a huge issue because the "function" is actually a React component with the following props: interface InputProps<Name extends string> {
name: Name;
value: Date;
onFieldChanged: (update: { [key in Name]: string; }) => void;
} The goal was to have a typesafe "link" between the value of the <Input
name="foobar"
value={value}
onFieldChanged={ update => setValue(update.foobar) }
// This foobar is typechecked ^^^^^^
/> Manually specifying generic parameters of React-components isn't very common, so it's kind of an implicit pinky-promise, or what to call it. But yeah, it bugs me that there's no way to be properly explicit about this. |
See #27808 |
@RyanCavanaugh That talks about intersections of different types though. Not sure how it would solve this, since |
Yeah, presumably we'd allow |
Then I'll close this, as it would be solved by #27808 🙂👍 |
TypeScript Version: 4.0.2
Search Terms:
generic known key extends string
Expected behavior:
I should be allowed to call
fn
with an object matching the expected type.Actual behavior:
Getting the following error when trying to call
fn
, even though the object I pass in is matching the type.The usage of the function shows that the callback, at least seemingly, is typed correctly. I.e. I am allowed to access
obj.foobar
, but notobj.somethingElse
.Related Issues:
I got a link to #13948 when asking about this on https://stackoverflow.com/q/64139075/39321, but I don't understand "Typescript lingo" or the more complicated React issue well enough to know if it's actually talking about the same issue as what I've run into here.
So I decided to post this as a bug. If it actually is the same, close this as a duplicate (and I'll know for sure), and if it's not, great, you have a new bug.
I'm guessing it might be reported before too though, but I really don't know what the correct name for this issue is, so I find it impossible to search for it. 😕
Code
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: