jeudi 16 septembre 2021

Check some data for containing fields for some interface (Typescript)

I want to find solution for API (nodejs). When I get request with any data, I want to handle this data for specific model (interface, type etc.).

This repo descriptions perfectly my case. How can I get same result without changes typescript-compiler?

I tried to use reflection, but my knowledge of typesctipt isn't enough.

My example code logic:

type Person = {
    name: string,
    age: number,
    address: {
        city: string,
        street: string,
        house: number,
    },
};

const input_data = {
    name: 'John',
    age: 23,
    address: {
        city: 'NY',
        street: "Grand Street",
        house: 15,
    },
}


function check (data: any, InterfaceObjTitle: string): boolean {
    // *getting type InterfaceObjTitle (current case person) from the variable*
    // * cycle for checking fields of InterfaceObjTitle within data
}

check(input_data, 'Person ');




Aucun commentaire:

Enregistrer un commentaire