#Extends clause + Pick = invalid property without type union addition?

1 messages · Page 1 of 1 (latest)

raw obsidian
#

I wanted to know why I had to append & IRedirectRow to my extends clause in order for Pick to actually, er, pick up the keys in the following:

interface IRedirectRow extends QueryResultRow {
    lookup_key: string;
    url: string;
}

type RedirectRow<T = unknown> = (T extends keyof IRedirectRow
    ? Pick<IRedirectRow, T>
    : IRedirectRow) &
    IRedirectRow;

const result = getRedirectRow();

// url is a valid property with the "& IRedirectRow" appendage
// url is NOT valid once you remove the appendage, why?
console.log(result.url);

The moment I remove the appendage, the inferred return type changes from Pick<"lookup_key" | "url"> to Pick<"lookup_key"> | Pick<"url"> and I don't understand why that happens. Reading the extends clause docs now but it's not clicking.

viral grove
#

!hb distributive

vernal lotusBOT
raw obsidian
#

Perfect, thank you so much. Very interesting quirk but the explanation makes total sense. Thank you so much! 💞

#

!resolvexc

#

Whoops, I can spell.