Skip to content

"No repeated property names" error in object literals is duplicated in strict mode #46815

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

Closed
sandersn opened this issue Nov 15, 2021 · 4 comments · Fixed by #46929
Closed

"No repeated property names" error in object literals is duplicated in strict mode #46815

sandersn opened this issue Nov 15, 2021 · 4 comments · Fixed by #46929
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Good First Issue Well scoped, documented and has the green light Help Wanted You can do this
Milestone

Comments

@sandersn
Copy link
Member

In strict mode (strict: true, inside a class or inside a module), duplicated property names get two errors instead of one:

// @strict: false
const first = { a: 1, a: 2 }
class C {
    m() {
        const second = { a: 1, a: 2 }
        return second.a
    }
}

Expected: one error on each a: 2.
Actual: two errors on the a: 2 inside the class:

  1. Diagnostics.An_object_literal_cannot_have_multiple_properties_with_the_same_name_in_strict_mode
  2. Diagnostics.Duplicate_identifier_0

In fact, the first error is better, but isn't used for first;

Expected: On firsts a: 2, the error should be "An object literal cannot have multiple properties with the same name."
Actual: Duplicate identifier 'a'.

This inconsistency arose because Typescript originally targetted ES3 and ES5; duplicate property names are only an error in ES5's strict mode. In fact, they're not even an error in ES2015+'s strict mode. But this is a good error, and it should apply everywhere.

Expected: The error should be "An object literal cannot have multiple properties with the same name."
Actual: "An object literal cannot have multiple properties with the same name in strict mode."

Thanks to https://stackoverflow.com/questions/36321790/js-two-or-more-object-properties-with-the-same-name-in-non-strict-mode for pointing out the spec changes.

@sandersn sandersn added Bug A bug in TypeScript Good First Issue Well scoped, documented and has the green light Help Wanted You can do this labels Nov 15, 2021
@sandersn sandersn added this to the Backlog milestone Nov 15, 2021
@sandersn sandersn added the Domain: Error Messages The issue relates to error messaging label Nov 15, 2021
@fatcerberus
Copy link

In strict mode (strict: true, inside a class or inside a module)

Wait, is this talking about JavaScript strict mode, or TypeScript strict: true?

@sandersn
Copy link
Member Author

Typescript's approximation of Javascript's strict mode. I think the flag is actually alwaysStrict: true, and strict: true includes it. But classes and modules also put their contents into strict mode.

@fatcerberus
Copy link

classes and modules also put their contents into strict mode

Yep, I knew this, but then you mentioned strict: true (i.e. strict type checking mode) and got confused as to which one you were referring to. Classes and modules don't automatically enable all the strict type checker options, as far as I'm aware. :)

@Yuuki77
Copy link
Contributor

Yuuki77 commented Nov 27, 2021

I will work on this issue 💪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Good First Issue Well scoped, documented and has the green light Help Wanted You can do this
Projects
None yet
3 participants