-
Notifications
You must be signed in to change notification settings - Fork 12.8k
in
operator narrowing (key in obj
) doesn't work properly when key type is a string union
#55561
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
Comments
Possible duplicate of #43284, since you seem to be trying to narrow the key and not the object.
|
I think this is pointing out an implementation detail of #50666 where objects get narrowed with declare let obj: object;
declare let oneKey: 'a';
declare let oneOfTwoKeys: 'a' | 'b';
if (oneKey in obj) {
obj.a; // ok
}
if (oneOfTwoKeys in obj) {
obj.a; // error
obj.b; // error
} I think the consistency that @barroudjo would like to see doesnβt necessarily hinge on narrowing the key type. You could imagine the second |
I'm actually surprised that declare let o: { foo?: string };
declare let k: "foo";
if (o[k]) {
let v: string = o[k]; // error
} |
That example narrows if That only strengthens the underlying point that our logic around this is not super consistent. |
Yeah my point is indeed one of consistency. I can't fathom why it should work for a string litteral, but not a union of string litterals. |
Sorry to bump it, but I'd like to know if this is a bug, or an expected behavior of typescript. If it's a bug I'll just use a hack and keep the code as close to this as possible, with a If it's the expected behavior, then I'll rack my brains to find a different solution ;-) |
Itβs a Possible Improvement. I wouldnβt wait on a solution if I were you. |
@andrewbranch thanks for the answer ! |
π Search Terms
in operator narrowing union
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?ssl=39&ssc=4&pln=20&pc=1#code/MYewdgzgLgBAYgeQQfQNIFECaBlGBeGAbQHIAzYgGhmJGIF0YBDCGUSKAbgFgAoN6eEjRZsyBADkAMpjHj0+ImXpMW-TrwCWYKAFMATqUbAd8ECBgBvXjBsxSALhjQ9WgObcetmCEfO3HgF9eXjUYXQECAAotAAcAVyhHODMYAB9LAIBKfAA+S2tbRBQMHAA6UhA9dCMAC0jIgGsdAE9svDyrTy9bDVIYRpaYLSGweKhs-hAAGx1SqZBXaNGEwibmukyPLyytwqES0QlpWXRyyurgOoHW3Pyu7qG+65GRsYnwCGnZ+cXYlbWNrsbDteAEPMEeAB6SFhPTNNxhcx6HQxPQgAAmcWMYRqJlRIAARjMALYwADuGigNScGmJMRmehgcQgJmAzB0EHsEOhrHZMAAjCEPrADvyFCRGJRqATlMxWMKPKFCQArMUECyMRzESVUAlamVg3iis5VWrPdp3Ly9fprF4q-nvSBfOYLSL21YtQGgzbcmFslkwABMQvYMAOgfF2qlxBlDDlakVwu8BOVEfVmuoksNPHDJoudVtFs6VqetuGKsDjs+MxdiwrHvWmxgPIAKrh9Gi9CwybiwENYBAaiA4lN0WBiFAqHKySZGMimRAEQADLRLmBgOdoilgVxOcxUxiwKkmGPEGD4mL6KDNJjAYwQFjokAc8ewGqMGKXsDe8FQv18gBmEMBAOADI1jFR5XYRNQxVMD0y1LMOGbGFQJgJ8OXXEBYDYKBGGGRgwBvNYWBAPo4KNEQALzM1Cw6AobGtZ5yxTACq2dH43VYhtARQx4YDbGAO0qFhhn9ExAyoY8+0pJwhxHdEYFxecoBSAkEn7dDnwgV8YAAQgMn8gA
π» Code
π Actual behavior
in
narrowing so that there is no incorrect 'b' property accessπ Expected behavior
Typescript shouldn't detect any errors in case 2. The type narrowing with the
in
operator ensures that we are only accessing properties that actually exist.Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: