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

How to dynamically make properties of a dynamic type optional?

I know there is a way to generate a type dynamically: Typescript dynamically create types based on object

But I can’t find a way to make some properties of the dynamically generated type optional.

For example, say I have a object

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

const obj = {
  keyName1: {
     type: 'string',
     required: true
  },
  keyName1: {
     type: 'string',
     required: false
  },
}

How do I make a type based on this object? Given that some properties are optional (if required is false).

If I simply have this object:

const obj = {
  keyName1: 'string'
}

I could make a type with:

  type Generate<T> = T;
  type Generated = Generate<typeof myObj> 

But with optional parameters, I don’t know how…

>Solution :

You’ll first need a utility type mapping each type string (eg 'string') to its type (string).

Then, map the object properties and do Obj[K]['required'] extends true ? to conditionally check whether to alternate with undefined or not. See this answer for how to make it optional.

const obj = {
  keyName1: {
    type: 'string',
    required: true
  },
  keyName2: {
    type: 'number',
    required: false
  },
} as const;
type Obj = typeof obj;
type TypesByTypeof = {
    'string': string;
    'number': number;
}

type ObjType = {
  [key in keyof Obj as Obj[key]['required'] extends true ? key : never]: TypesByTypeof[Obj[key]['type']]
} & {
  [key in keyof Obj as Obj[key]['required'] extends false ? key : never]?: TypesByTypeof[Obj[key]['type']]
}

Result:

type ObjType = {
    readonly keyName1: string;
} & {
    readonly keyName2?: number | undefined;
}
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