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

Incorrect type checking when object is created using spread operator #60610

Closed
bacher opened this issue Nov 26, 2024 · 2 comments
Closed

Incorrect type checking when object is created using spread operator #60610

bacher opened this issue Nov 26, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@bacher
Copy link

bacher commented Nov 26, 2024

🔎 Search Terms

"object is created using spread operator"
"spread operator type mismatch"
"create object based on another object"
"incorrect inference when spread operator used"

🕗 Version & Regression Information

  • This is the behavior in every version I tried. 5.*, 4.*, 3.*.

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.2#code/C4TwDgpgBA0hIDkCGBbaBeKByJWoB9sAjLAbgChyAzAVwDsBjYASwHs6oBzCYAZVbRwQACgCUALljxkaKAG9yUKACceNZRxxlyAX0qhIUACJJgSKJgBKEBq2UATADxCZEADRQAzsGXM6nAD4Kcls6byhWAEZJEzMLeUUoJEksAAsIABsM1iw3RKIUgHc7DPtc3WDQ8NYAJhjTc0wFJQA6Nqi8pQB6LqgAQU9PZk46NDpgL1TWGlKoKiRmDMSAbW4+AQghMQBdSQBWPJ1gnqgAFVTmTygMv2hPKZn7KAhlZTtxclqWpBbgVgBVMCQZQAYSQnggYgoJwAegB+ShfIi-AFAl5giFQ8iwhFAA

💻 Code

type KeyName = 'a' | 'b';

function getSomeKey(): KeyName {
  return 'a';
}

type Data = Record<KeyName, string>;

const o1: Data = {
  a: 'hello',
  b: 'world',
};

const o2: Data = {
  ...o1,
  // Assignment should fail
  [getSomeKey()]: 5,
};

// This line should error:
o2.a.toUpperCase();
// ^?

o2.b.toUpperCase();
// ^?

🙁 Actual behavior

This code snippet effectively puts number 5 into a field, which TS considers a string.

🙂 Expected behavior

const o2: Data = {
  ...o1,
  // Assignment should fail
  [getSomeKey()]: 5,
};

This lines should fail.

Additional information about the issue

This problem reproduced even without using of explicit Record type, example: https://www.typescriptlang.org/play/?ts=5.7.2#code/GYVwdgxgLglg9mABAcwKZQMpwLaoNKoCeAFAJQBciA5AIZWIA+1ARvQN4BQiiATuiDyS0qAbg4BfDhwgIAzlERwAjIgC8iTtxqUqAC1QAbA3CoAaLomY6A7nB4GAJmYmIasxDLDyx0uQrgATGoaFgB04crm3AD00YgAgrKyMMhguGAKsrpwII6IwDQwBhYA2miYOPhEZAC6lACs5uI+sYgAQnBQuorAiF2osqiIWTl5qDw8duQcgaE0oVBwAKoADivjAMJuqGRirQB6APxSs8wLy2ub27scB8dAA

@MartinJohns
Copy link
Contributor

Duplicate of #32236.

@jakebailey jakebailey added the Duplicate An existing issue was already created label Nov 26, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants