cookiestore

Do the return types specified in MDN documentation denote that a value will always exist, unless specified otherwise?


I'm looking that the MDN documentation for the the cookieStore.get function.

CookieStore is not yet typed by TypeScript so I need to create my own definition.

The question I have is in the MDN documentation MDN denotes optional parameters for functions - but on the return type I'm yet to see optional values on the return type. Does this mean I can assume that all the values listed on the cookieStore.get should/will exist?


Solution

  • Based on the W3C specification, it doesn't seem to be the case that the MDN documentation is necessarily accurately describing the return type of cookieStore.get()

    The specification defines this type:

    dictionary CookieListItem {
      USVString name;
      USVString value;
      USVString? domain;
      USVString path;
      DOMHighResTimeStamp? expires;
      boolean secure;
      CookieSameSite sameSite;
      boolean partitioned;
    };
    

    Note that, according to that, the domain and expires properties are marked as optional, which is not mentioned on MDN.

    If we convert this to TypeScript, we would get something like this:

    type CookieListItem = {
      name: string;
      value: string;
      domain?: string;
      path: string;
      expires?: number;
      secure: boolean;
      sameSite: 'strict' | 'lax' | 'none';
      partitioned: boolean;
    };
    
    const cookie: Promise<CookieListItem | null> = cookieStore.get('');