Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Typescript flattens array types to their element type not working inside Mapped Types

I have User type like below

interface IUserMongo {
  name: string;
  age: number;
  address: string[];
  nested: {
    name: string;
    sayYes: boolean;
    rrr: 1;
  };
}

I want to derive the following type

interface IUserMongo {
  name: number;
  age: number;
  address:number;
  nested: {
    name: number;
    sayYes: number;
    rrr: number;
  };
}

code is working except for arrays.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

error: Type ‘number’ is not assignable to type ‘number[]’

ts playground link

interface IUserMongo {
  name: string;
  age: number;
  address: string[];
  nested: {
    name: string;
    sayYes: boolean;
    rrr: 1;
  };
}

type IUserProject<T, N = number> = {
  [K in keyof T]: T[K] extends object ? IUserProject<T[K], N> : N;
};

const projection: IUserProject<IUserMongo> = {
  name: 1,
  age: 1,
  address: 1,
  nested: {
    sayYes: 1,
    name: 1,
    rrr: 1,
  },
};

const user: IUserMongo = {
  name: 'sdg',
  age: 1,
  address: ['gfg'],
  nested: {
    sayYes: true,
    name: 'rrr',
    rrr: 1,
  },
};

// this is the behavior i want
interface MultiContainer {
  Item1: string[];
  Item2: number[];
}

type SingleContainer = { [P in keyof MultiContainer]: number };

const tt: SingleContainer = {

    Item1: 1,
    Item2: 1,
};

>Solution :

Check if it’s an array:

type IUserProject<T, N = number> = {
  [K in keyof T]: T[K] extends object ? T[K] extends ReadonlyArray<unknown> ? N : IUserProject<T[K], N> : N;
};

Works now

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading