It seems that when a class is used as a type, only keys of that class should be allowed. However, assigning [Symbol()]
, [String()]
, or [Number()]
as property keys does not result in an error, allowing incorrect properties to be assigned. An even more curious case is that if [Symbol()]
is assigned to a variable before being used as a property key, it causes an error as expected. This is different from the behavior seen when assigning [String()]
or [Number()]
to a variable first.
const sym = Symbol()
const str = String('another unwanted prop')
const num = Number(1)
class A { // <-- can be class/type/interface
a?: string
b?: number
}
let a: A = {
[Symbol()]: 'anything',
[String('unwanted prop')]: 'anything',
[Number(0)]: 'anything',
[str]: 'anything',
[num]: 'anything',
[sym]: 'anything' // <-- why does this error while [Symbol()], [String()] and [Number()] don't?
// ~~~~~~~~~~~~~~~~~
}
This behavior does not match my expectations and I find it somewhat confusing.
Is this considered an issue or is it the intended behavior? Am I overlooking something?