0

I want to filter only the key and values which is in the type of document model in typescript example below:

type DocumentModel = {
  id?: string | null,
  docType: DocType,
  accountId?: string | null,
  updatedBy: string | null;
};

whatever the JSON object is, I need to filter only these values in the output

input json

{
    "id": "80e474bc-7e28-4e28-ad43-c031da0f297b",
    "docType": "80e474bc-7e28-4e28-9b11-2259cf68f91e",
    "type": "INDIVIDUAL",
    "accountId": "80e474bc-7e28-4e28-ad43-c031da0f297b",
    "account": {
               "id": "80e474bc-7e28-4e28-ad43-c031da0f297b",
               "type": "INDIVIDUAL",
               "updatedAt": "2023-04-05T11:53:17.587Z"
                },
    "attributes": "{\"identity\":\"12345\",\"name\":\"nicky\"}",
    "questionnaire": null,
    "owner": "bddfe953-0c1c-4aa4-9b11-2259cf68f91e",
    "comments": null,
    "updatedBy": "bddfe953-0c1c-4aa4-9b11-2259cf68f91e",
    "createdAt": "2023-04-05T11:53:17.587Z",
    "updatedAt": "2023-04-05T11:53:17.587Z"
}

I want only the below output

{
"id": "80e474bc-7e28-4e28-ad43-c031da0f297b",
"docType": "80e474bc-7e28-4e28-9b11-2259cf68f91e",
"accountId": "80e474bc-7e28-4e28-ad43-c031da0f297b",
"updatedBy": "bddfe953-0c1c-4aa4-9b11-2259cf68f91e",
}
  • Typescript only offers static type analysis. At runtime (which is when you need to actually filter data) all type information is lost. – apokryfos Apr 05 '23 at 15:36
  • Not sure if this still works, but it might be worth a try [Get keys of a Typescript interface as array of strings](https://stackoverflow.com/questions/43909566/get-keys-of-a-typescript-interface-as-array-of-strings) – derpirscher Apr 05 '23 at 15:39
  • You might find zod handy here, it allows you do define your types so that they work at runTime & designTime, link -> https://github.com/colinhacks/zod – Keith Apr 05 '23 at 15:58

0 Answers0