#What does `infer` means inside Mapped Types?

1 messages · Page 1 of 1 (latest)

pulsar fern
#

Recently I stumbled this code

type SmooshedObjectUnion<T> = {
    [K in T extends infer P ? keyof P : never]: T extends infer P
        ? K extends keyof P
            ? P[K]
            : never
        : never
}

It combines all properties of objects consisting in union of object
so { foo: number } | { bar: number } => { foo: number, bar: number }

However i have trouble understanding this code, mainly:

  1. What is the difference between
    [K in T extends infer P ? keyof P : never] and [K in keyof T] ?

  2. What is infer P doing before the question mark?
    I understand what it does if this pattern is the context: A extends B ? infer C but i dont understand what it do if it is placed before the quetsion mark. Is P simply just a temporary variable? what is its purpose?

  3. How to read the whole [] part?
    Is it (K in T) extends infer... or K in (T extends infer ...)
    Is the "extends" part of K in T or just T?

  4. What is with the extra conditional generics on the value of the mapped value?
    Is it just checking if K exists within the possibilities of P? Just looking for a breakdown in general

#
type DistributedIdentity<T> = {
  [K in T extends unknown ? keyof T : never]: T extends { [_ in K]: infer V }
  ? V
  : never
}

This is more readable but does the same thing it seems. But I still dont understand if its read as
K in T extends unknown ?
or
T extends unknown ?

#

I dont like how this code can't be broken down step by step

#

or atleast im not able to

robust crown
#

infer is a part of conditional types, not mapped types. this is K in (T extends infer P ? keyof P : never)

robust crown
#

A extends B ? infer C
this isn't a thing, btw. infer needs to exist in the B part

robust crown
#

to more directly answer your questions;

  1. #1208356457146486815 message
  2. it's not doing anything useful here.
  3. the latter: #1208356457146486815 message , see AST
  4. like 1. #1208356457146486815 message
#

also, "conditional generics" isn't really a thing, those are just conditionals/conditional types
generics are a separate thing

pulsar fern
# robust crown > `A extends B ? infer C ` this isn't a thing, btw. `infer` needs to exist in th...

You're right.
type Flatten<Type> = Type extends Array<infer Item> ? Item : Type;
This is the usage of infer im more familiar with.

Lets say this code

type DistributedIdentity<T> = {
  [K in T extends unknown ? keyof T : never]: T extends { [_ in K]: infer V }
  ? V
  : never
}

I'm still not sure why it needs to be T extends unknown ? keyof T : never / why it does not work with just keyof T

Also that applying the types directly by substituting T does not work
Playground

robust crown
#

I'm still not sure why it needs to be T extends unknown ? keyof T : never / why it does not work with just keyof T
#1208356457146486815 message

#

!hb dist

summer slateBOT
robust crown
#

Also that applying the types directly by substituting T does not work
right, distribution only works over type params and inferred types

pulsar fern
#

aahhhhhhh