Skip to content
TheChatArchiveThe Chat Archive
o3-2025-04-16· OpenAIHugging Face LMArena Human Preference 140K

you vite expert. if: status: 'pending' 'stopped' ; and: export const MESSAGESTATUSES = {…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-opus-4-20250514-thinking-16k’s answer.
  1. User

    you vite expert. if: status: 'pending' | 'stopped' ;

    and: export const MESSAGE_STATUSES = { PENDING: 'pending' };

    how to make status = MESSAGE_STATUSES

  2. Assistant

    In TypeScript you can “extract” the literal union directly from the object so you never have to repeat yourself.

    // 1. Declare the object and freeze its values ⬇️
    export const MESSAGE_STATUSES = {
      PENDING: 'pending',
      STOPPED: 'stopped',
    } as const;       // <-- `as const` is the magic
    
    // 2. Infer a union‐type from the object’s values ⬇️
    export type MessageStatus = typeof MESSAGE_STATUSES[keyof typeof MESSAGE_STATUSES];
    //               ^?        --> "pending" | "stopped"
    
    // 3. Use it everywhere ⬇️
    let status: MessageStatus = MESSAGE_STATUSES.PENDING; // ✅ ok
    status = MESSAGE_STATUSES.STOPPED;                   // ✅ ok
    status = 'foo';                                      // ⛔️ type-error
    

    Explanation

    1. as const
      • Makes every property readonly.
      • Narrows each value to its literal ('pending' instead of string).

    2. typeof MESSAGE_STATUSES gives you the object’s type.
      keyof ... gets the keys ("PENDING" | "STOPPED").
      Indexing with those keys ([...]) pulls out the values, i.e. the literal union we want.

    That’s it—status is now guaranteed to be either 'pending' or 'stopped', and you never have to maintain two separate lists.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.