#Different approach to dealing with a value that can be multiple types

6 messages · Page 1 of 1 (latest)

stuck radish
#
{(results.length > 0 || query.length > 0
        ? results
        : championData.quotes
      ).map((item) => {
        let quote = '',
          url = '';
        if ('quote' in item) {
          quote = item.quote;
          url = item.url;
        }
        if ('obj' in item) {
          quote = item.obj.quote;
          url = item.obj.url;
        }
        return (
          <div key={quote + url}>
            <p>{quote}</p>
            <audio controls src={url} />
          </div>
        );
      })}

item: Quote | Fuzzysort.KeyResult<Quote>

#

whats are some other ways i can do this.
this feels awkward

knotty hill
#
const { quote, url } = 'obj' in item ? item.obj : item
stuck radish
#

🤦‍♂️

lapis kite
#

can't you turn the Fuzzysort.KeyResult<Quote> into a Quote first if it is one

#

then work from that?