Skip to content
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

Duplicate keys do not error #59396

Open
crazyair opened this issue Jul 23, 2024 · 7 comments
Open

Duplicate keys do not error #59396

crazyair opened this issue Jul 23, 2024 · 7 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@crazyair
Copy link

crazyair commented Jul 23, 2024

🔎 Search Terms

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions latest and latest_
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

No response

💻 Code

export const func = (demo: string) => {
    const obj = {
        [demo]: '1',
        [demo]: '1',
    };
    console.log(obj);
};

🙁 Actual behavior

no error
image

🙂 Expected behavior

error

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

Related: #43186

@jcalz
Copy link
Contributor

jcalz commented Jul 23, 2024

Presumably this isn't a bug but a limitation, since TS doesn't track identity of variables here. If the type of demo is string then the two utterances of demo are seen by TS as just two string-valued computed keys and therefore it won't complain (otherwise it would complain on {[demo1]: 0, [demo2]: 0} which we wouldn't want).

@crazyair
Copy link
Author

Presumably this isn't a bug but a limitation, since TS doesn't track identity of variables here. If the type of demo is string then the two utterances of demo are seen by TS as just two string-valued computed keys and therefore it won't complain (otherwise it would complain on {[demo1]: 0, [demo2]: 0} which we wouldn't want).

ok,After mentioning it, I found that this is indeed the case, but there is no recommended writing that can avoid this problem. Because in this code, found a problem, only to find the key duplicate, was overwritten. It's hard.

@MartinJohns
Copy link
Contributor

@jcalz Presumably it could check if the identifier is used twice in the object initialization. But I don't know how realistic this issue is and how expensive this check would be.

@RyanCavanaugh
Copy link
Member

We could error on the same identifier; the odds a prior property initializer really mutate it seem low enough. But this also seems like a fairly rare mistake to make and would incur a relatively high cost to keep track of all the identifiers and check for uniqueness. We'd need to see more than a handful of instances of this particular problem happening and see the larger context of the code (maybe there's a better check, hard to say).

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Jul 23, 2024
@crazyair
Copy link
Author

We could error on the same identifier; the odds a prior property initializer really mutate it seem low enough. But this also seems like a fairly rare mistake to make and would incur a relatively high cost to keep track of all the identifiers and check for uniqueness. We'd need to see more than a handful of instances of this particular problem happening and see the larger context of the code (maybe there's a better check, hard to say).

eslint Performance will be better?

@crazyair
Copy link
Author

image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants