You can tell TypeScript to treat a string value as a literal type using the as const phrase.

For example, if you have an interface like this:

export interface Message {
  role: "user" | "assistant" | "tool";
  content: string;
}

You can add objects to it like this:

const updatedMessages: Message[] = [
      ...currentMessages,
      {
        role: "assistant",
        content: baseResponse,
      },
      ...(functionResponse
        ? [{ role: "assistant" as const, content: functionResponse }]
        : []),
    ];