vs code screenshot
Typescript does NOT show errors or warnings when accessing undefined property:
export type Chat = {
id: string;
members: ChatUser[];
name?: string;
messages?: Message[] | undefined;
order?: string;
_count: { messages: number };
};
const newChat: Chat = {
id: "1",
members: [{ id: "1", login: "test", isOnline: true }],
_count: { messages: 0 },
};
const lastMessage = newChat.messages[0];
const isRead = lastMessage.isRead;
BUT I get lastMessage is undefined
in runtime.
what can i do to catch undefined value before runtime?
I tried explicitly setting the type of lastMessage to Message | undefined
but I still get const lastMessage: Message
from Typescript