首页 > 解决方案 > 检查一些数据以包含某些接口的字段(Typescript)

问题描述

我想找到 API (nodejs) 的解决方案。当我收到任何数据的请求时,我想为特定模型(接口、类型等)处理这些数据。

这个回购完美地描述了我的情况。如何在不更改 typescript-compiler 的情况下获得相同的结果?

我尝试使用反射,但我对 typesctipt 的了解还不够。

我的示例代码逻辑:

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 ');

标签: node.jstypescriptapioopreflection

解决方案


推荐阅读