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
No excess property error for computed property #36920
Search Terms: excess property, computed property, index signature
Code
interfaceFoo{a: string;}constbar: Foo={a: "",z: ""// error, excess prop}constbaz: Foo={a: "",["Z".toLowerCase()]: ""// this is okay I guess?}
Expected behavior:
The computed property in the definition of baz should maybe produce an excess property error, since Foo has no string index signature and "Z".toLowerCase() is only known to be a string.
Actual behavior:
The computed property is allowed.
This issue is a re-opening of #22427, which was marked as a bug and closed as "fixed", but I don't see an actual fix referenced anywhere in that issue and the behavior described in that issue persists. Any ideas? Thanks!
There's an SO question asking this, which brought me to #22427, which has me scratching my head 😕.
Reacted by Maciej Sikora, Karol Majewski, Timo Rossa, SancheZz, soffyo, BenLorantfy-AB and Hans Brende
Is the proposed rule here that all computed properties are disallowed, or just that any computed property is disallowed if all the known keys of the target type have already been written?
If computed property keys are string or widened to string then I'd think there should be an excess property warning since the compiler can no longer track whether the properties are valid at all.
In the "a" | "b" case, currently these are widened to string, which is itself a bug: #13948 If it stayed "a" | "b" then I'd expect {[Math.random()>0.5 ? "a" : "b"]: ""} to be of type {a: string} | {b: string} and whether that triggers any warning when combined with other properties is an interesting question but maybe not directly relevant for this particular issue. Still, my instinct is:
don't issue a "multiple property" warning like you'd get with {a: 1, a: 1};
if any of the keys in the union are unknown then issue an excess property warning; const f: Foo = {a: "", b: "", [Math.random()<0.5 ? "c" : "d"]: ""}; // error! c and d are extra, you are bad
if any of the keys are not otherwise set then issue a "this property might be missing" warning: const f: Foo = {"b": "", [Math.random()<0.5 ? "a" : "b"]: ""}; // error! a might be missing, not necessarily a Foo
So can I take it that #22427 was misclassified as a bug since we're treating it here as a suggestion? I'm fine either way; I mostly want to have an as-authoritative-as-possible answer to the linked SO question.
Activity
RyanCavanaugh commentedon Feb 21, 2020
Is the proposed rule here that all computed properties are disallowed, or just that any computed property is disallowed if all the known keys of the target type have already been written?
RyanCavanaugh commentedon Feb 21, 2020
e.g. this doesn't seem wrong per se:
jcalz commentedon Feb 21, 2020
If computed property keys are
stringor widened tostringthen I'd think there should be an excess property warning since the compiler can no longer track whether the properties are valid at all.In the
"a" | "b"case, currently these are widened tostring, which is itself a bug: #13948 If it stayed"a" | "b"then I'd expect{[Math.random()>0.5 ? "a" : "b"]: ""}to be of type{a: string} | {b: string}and whether that triggers any warning when combined with other properties is an interesting question but maybe not directly relevant for this particular issue. Still, my instinct is:{a: 1, a: 1};const f: Foo = {a: "", b: "", [Math.random()<0.5 ? "c" : "d"]: ""}; // error! c and d are extra, you are badconst f: Foo = {"b": "", [Math.random()<0.5 ? "a" : "b"]: ""}; // error! a might be missing, not necessarily a Foowhich means no warning for your above example.
jcalz commentedon Feb 23, 2020
So can I take it that #22427 was misclassified as a bug since we're treating it here as a suggestion? I'm fine either way; I mostly want to have an as-authoritative-as-possible answer to the linked SO question.
timonson commentedon Mar 6, 2020
Ryan Cavanaugh (@RyanCavanaugh) I also would like to know if this is considered to be a bug or not.
It seems kinda strange and inconsistent to me that
barthrows an error andbazdoesn't. Because the same thing happens in both examples in the end:jcalz commentedon Jul 17, 2023
Is #44794 related to this or not?
jcalz commentedon Jul 17, 2023
Analogous behavior happens with
symbolcomputed properties:Playground link