When working with an object type, we can access the type of one of its fields using bracket-string notation. However, why isn't it possible to use dot notation like in JavaScript? Could there be a conflict or some other reason for this limitation? I have a feeling that I am overlooking something quite basic.
type Foo = {value: number | string};
type Bar = Foo["value"]; // This works fine, Bar is a union of number and string
type Baz = Foo.value; // Error occurs here
The error message seems to suggest something about namespaces. But even if there was a namespace called Foo
, accessing Foo.value
should refer to a value rather than a type, making it not clear where the ambiguity lies.