Remove fields from typescript interface object

后端 未结 3 565
余生分开走
余生分开走 2020-12-11 05:11

I am getting a json response and storing it in mongodb, however the fields that I don\'t need are also getting in to the database, is there anyway to strip the uneseccary fi

3条回答
  •  执念已碎
    2020-12-11 05:38

    If you want to do this in a strongly-typed way, you can define a dummy/ideal object which satisfies your interface (const dummy: IMyInterface = {someField: "someVal"};), and then filter incoming objects' fields against Object.keys(dummy). This way your compiler will complain if you update the interface without updating this 'filtering' code.

提交回复
热议问题