-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Spread with enum index key, invalid members don't trigger errors #32236
Comments
Simplified type PropName = "A" | "B";
type MyMappedType = {
[key in PropName]: string;
};
declare const AB: PropName;
const k1: MyMappedType = {
A: "",
B: "",
// No error
[AB]: 10
};
const k2: MyMappedType = {
A: "",
B: "",
...{
// Also no error
[AB]: 10
}
};
const k3 = {
A: "",
B: "",
[AB]: 10
};
// No error
const j: MyMappedType = k3; |
So the problem here is that there's not a good way to describe the type of That said, ideally we'd have some way to flag the @ahejlsberg any thoughts? |
@RyanCavanaugh This seems to affect not just enums. If the computed property is
I really don't think the result of |
@dragomirtitian i think technically that’s ok because the resulting object may have other keys but still satisfies the interface that the object type specifies - though it’s also the case that applying that key-value pair may cause an existing key on the object to have an incorrect value type, which is unsound |
I am running into the same issue. Any update on this? It is fairly common to update an object using code similar to this: type Key = 'a' | 'b' | 'x'
type Obj = {[key in Key]: number}
const x = 'x' as Key
const obj: Obj = {
a: 4,
b: 4,
x: 4,
}
const updatedObj: Obj = {
...obj,
[x]: 'hello', // No error. No type check on the value, which is a string instead of number
} The same is true for const updatedObj: Obj = Object.assign({}, obj, { [x]: '4' }) // No error It seems that |
TypeScript Version: 3.5.1
Search Terms:
object spread enum index missing keys
Code
Playground link has code
Expected behavior:
Missing keys & incorrect value types when I build a nested object using the spread operator with index keys, ought to throw errors for invalid types
Actual behavior:
It seems like the child object can have whatever shape I want, the types aren't being checked at all
Playground Link: This is same as above playground link
Related Issues: I'm not really sure what the root limitation that's causing this is so it's hard for me to find similar issues.
The text was updated successfully, but these errors were encountered: