-2

There is a great answer by @jcalz on how to dynamically create interface: Typescript dynamically create interface

However I was wondering whether we could do the same with nested structure and integrate an optional parameter, see the ? below.

Example from

{"names": {"firstName": "string", "lastName": "?string"} , "age": "number" }

output

{name: {firstName: string, lastName?: string}, age: number}

1 Answer 1

1

Based on his answer you can define own types. So you can define there '?string' too. But I guess it will require to specify the property anyway with value of string or undefined.

type MapSchemaTypes = {
  string: string;
  integer: number;
  '?string': string | undefined;
}

I was managed to do it like that:

const pattern = {
  "names": {
    "firstName": "string", 
    "lastName": "?string"
  },
  "age": "number",
  "name": "string",
  "lastName": "?string",

} as const;

type FILTER_PROPS<Base, Condition> = {
    [Key in keyof Base]: Base[Key] extends Condition ? Key : never;
}[keyof Base];

type Pattern<T> = {
  [K in FILTER_PROPS<T, '?string' | '?number'>]?:
    T[K] extends '?number' ? number
    : T[K] extends '?string' ? string
    : never
} & {
  [K in FILTER_PROPS<T, 'string' | 'number' | {[SK in keyof any]: any}>]:
    T[K] extends 'number' ? number 
    : T[K] extends 'string' ? string 
    : T[K] extends {} ? Pattern<T[K]>
    : never
};

const a: Pattern<typeof pattern> = {
  names: {
    firstName: 'Test',
  },
  name: 'required',
  age: 123,
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.