#Trying to understand this return type

6 messages · Page 1 of 1 (latest)

lost compass
#

So I have the following code:

const obj = {
    'key1': ['a', 'b', 'c'],
    'key2': ['d', 'e', 'f'],
} as const

type Property = keyof typeof obj;
type PropertyValue<T extends Property> = typeof obj[T][number]

type Test0 = typeof obj extends Record<string, any[]> ? true : false; // false
type Test1 = typeof obj extends Record<string, string[]> ? true : false; // false

type Test2 = typeof obj['key1'] extends string[] ? true : false; // false

type Test3 = PropertyValue<'key1'> extends 'a' ? true : false; // false
type Test4 = PropertyValue<'key1'> extends 'a' | 'b' | 'c' ? true : false; // true

While I mostly understand why Test4 returns true, I do not understand why the other tests do not; especially Test3.
If anyone could help me understand, or at least point me in the right direction, I'd be grateful

serene tree
#

It can be helpful to read extends as assignable to

#

PropertyValue<'key1'> is "a" | "b" | "c" so Test3 reads as:

"a" | "b" | "c" /* assignable to */ "a" ?
#

In other words, is this valid?

onyx robinBOT
#
declare const abc: "a" | "b" | "c";
const a: "a" = abc;
//    ^
// Type '"a" | "b" | "c"' is not assignable to type '"a"'.
//   Type '"b"' is not assignable to type '"a"'.
lost compass