I'm looking for a TypeScript type definition that describes an object with a single property (having any value).
I know that thare are index signatures, e.g.
type X = { [key: string]: any }
or alternatively
type X = Record<string, any>
However this would allow an object like
const obj: X = {
"a": 12,
"b": "c"
}
I'm looking for a type Y that restricts obj to have a single property, representing kind of a "RecordEntry", that is
const obj: Y = {
"a": 12
}
should be fine but
const obj: Y = {
"a": 12,
"b": "c"
}
should be rejected by the compiler.
Is that even possible?
It sounds like what you want is simply
I don't believe there's a way to restrict to only one unspecified type. The semantics of such a thing could also be confusing. What if, for example, you had
Would that be a compiler error?