-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed as not planned
Closed as not planned
Copy link
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
🔎 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
💻 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
amitdahan
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created