I am looking to create a type
/interface
that can store properties of the same type as itself.
For Instance:
type TMessagesFormat = { [key: string]: string };
interface TMessages {
messages: TMessagesFormat;
}
interface TGroupMessages {
messages?: TMessagesFormat;
controls: { [key: string]: TMessages | TGroupMessages }
}
let groupMessages: TGroupMessages = {
controls: {
username: { messages: {required: 'Username required'} }
}
}
let messages: TGroupMessages = {
controls: {
username: { messages: { required: 'Username required' } },
passwordGroup: {
messages: { nomatch: 'Passwords doesn't match' },
controls: {
password: { messages: { required: 'Password required' } }
}
}
}
};
The type checking is functioning correctly for username and passwordGroup, but the controls in passwordGroup can currently be anything without triggering any errors from the TS compiler. Even if I add the property controls: 'whatever'
(which should not be a valid type) inside the username
object literal, the code still compiles without any warnings or errors. How is this possible? Thank you!