Value of type keyof Interface can be used to add arbitrary values to new instances of Interface, but only if the interface has multiple properties #49372
Labels
Duplicate
An existing issue was already created
Bug Report
🔎 Search Terms
keyof arbitrary (value | type)
🕗 Version & Regression Information
keyof
. In particular, I tried on nightly using the playground as suggested.⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
A variable
key: keyof MyInterface
can be used to create a new instance ofMyInterface
with values of arbitrary types not included in the interface. This is true unlessMyInterface
only has one property, in which case the error is reported.🙂 Expected behavior
The error described above should be caught regardless of the shape of the interface.
I am rather new to TypeScript, and one thing is unclear to me: In reference to the code above, I would like to be able to say something like
return { ...vals, [key]: (value in union of property types) };
(this is distinct from what I did say above, which isreturn { ...vals, [key]: (value NOT in union of property types) };
). However, it isn't clear that I should be able to, provided all property types aren't identical, since I don't know which propertykey
corresponds to. Clarification on that point, i.e. "What should I be expecting, anyway?", would be greatly appreciated.The text was updated successfully, but these errors were encountered: