You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
in operator narrowing (key in obj) doesn't work properly when key type is a string unionΒ #55561
// case 1constKEYS1=['a','b']asconst;constobj1={a: 'a',b: 'b'};KEYS1.forEach(key=>{if(keyinobj1)console.log(obj1[key]);});// case 2constKEYS2=['a','b']asconst;constobj2={a: 'a'};KEYS2.forEach(key=>{if(keyinobj2)console.log(obj2[key]);// TS errors when it shouldn't, as we are using `in` narrowing so that the 'b' property access doesn't happen});// case 3constKEYS3=['b']asconst;constobj3={a: 'a'};// KEYS3 does not contain any keys of obj3KEYS3.forEach(key=>{if(keyinobj3)console.log(obj3[key]);// if TS errors in case 2, then it should here too but it doesn't !!!});
π Actual behavior
in case 2 TS errors when it shouldn't, as we are using in narrowing so that there is no incorrect 'b' property access
in case 3 TS doesn't give any errors, when consistency disctates that it should if it did in case 2...
π 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.
Possible duplicate of #43284, since you seem to be trying to narrow the key and not the object.
in operator narrowing is heuristic in nature and consistency isn't the main driving factor in how it works. You have #15256 and #50666 which narrow the object type in different ways, none of which do anything to the key.
I think the consistency that Jonathan Loss (@barroudjo) would like to see doesnβt necessarily hinge on narrowing the key type. You could imagine the second if statement narrowing the type of obj to Record<"a" | "b", unknown>βor { a?: unknown, b?: unknown } to avoid issues with exactOptionalPropertyTypes. Iβm not sure if thereβs a reason why something like this wasnβt considered... getting the feeling I could be missing something obvious though.
I'm actually surprised that oneKey in obj narrows at all, tbh. I always assumed the rule was that the LHS had to be an actual literal, not just literal-typed, as is the rule elsewhere:
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 TODO comment explaining the situation and that we're waiting for the bug to be solved.
If it's the expected behavior, then I'll rack my brains to find a different solution ;-)
Activity
jcalz commentedon Aug 29, 2023
Possible duplicate of #43284, since you seem to be trying to narrow the key and not the object.
inoperator narrowing is heuristic in nature and consistency isn't the main driving factor in how it works. You have #15256 and #50666 which narrow the object type in different ways, none of which do anything to the key.andrewbranch commentedon Aug 29, 2023
I think this is pointing out an implementation detail of #50666 where objects get narrowed with
inif the LHS is a string literal but not if the LHS is a union of two string literals, and it doesnβt look obvious at first glance why those canβt be treated similarly. Minimal repro: https://www.typescriptlang.org/play?#code/CYUwxgNghgTiAEEQBd4HsBGArAXO74yA3ALABQoksCSqaAdiANIgCeeA5FB6ReNHEQp0jAPIAzACoB3NC1YBnTt3gAfeBww9y5AJbj4ACgbM28XfXxYAlPADe5eE6sA6KLwC+eg8bFTZ8grmlpg29o7OoW68zq4YnuRAAI think the consistency that Jonathan Loss (@barroudjo) would like to see doesnβt necessarily hinge on narrowing the key type. You could imagine the second
ifstatement narrowing the type ofobjtoRecord<"a" | "b", unknown>βor{ a?: unknown, b?: unknown }to avoid issues withexactOptionalPropertyTypes. Iβm not sure if thereβs a reason why something like this wasnβt considered... getting the feeling I could be missing something obvious though.fatcerberus commentedon Aug 29, 2023
I'm actually surprised that
oneKey in objnarrows at all, tbh. I always assumed the rule was that the LHS had to be an actual literal, not just literal-typed, as is the rule elsewhere:andrewbranch commentedon Aug 29, 2023
That example narrows if
kis aconstinstead of aletπThat only strengthens the underlying point that our logic around this is not super consistent.
barroudjo commentedon Aug 29, 2023
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.
barroudjo commentedon Sep 4, 2023
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
TODOcomment explaining the situation and that we're waiting for the bug to be solved.If it's the expected behavior, then I'll rack my brains to find a different solution ;-)
andrewbranch commentedon Sep 5, 2023
Itβs a Possible Improvement. I wouldnβt wait on a solution if I were you.
barroudjo commentedon Sep 5, 2023
Andrew Branch (@andrewbranch) thanks for the answer !