typescript - Define return type based on input values - Stack Overflow

I'm trying to to connect to a 3rd party function, which will return an object typed as any, but it

I'm trying to to connect to a 3rd party function, which will return an object typed as any, but its return type is actually known based on the param outputParams passed into it.

As an example:

interface OutputParam {
  name: string;
}

// This function is external and out of my control - no changes can be made to this method
const executeStoredProc = (outputParams: OutputParam[]): object => {
  const retval = {};
  for (let output of outputParams) {
    retval[output.name] = 'sample data';
  }
  return retval;
}

const example = () => {
  const outputParams: OutputParam[] = [
    { name: "Sandwich_ID" },
    { name: "Sandwich_Has_Ketchup" },
  ];

  const exampleResult = executeStoredProc(outputParams);

  // !!! GOAL: exampleResult should recognize that `Sandwich_ID` and
  // `Sandwich_Has_Ketchup` are valid properties of exampleResult
  const sandwichId = exampleResult.Sandwich_ID;
  const sandwichHasKetchup = exampleResult.Sandwich_Has_Ketchup;

  return {
    sandwichId,
    sandwichHasKetchup,
  };
};

In the example above, is there any way for me to cast the exampleResult object to the type I expect it to be based on the output params? With the example above, that type would look like:

type exampleOutput {
  Sandwich_ID: unknown,
  Sandwich_Has_Ketchup: unknown,
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745110886a4611844.html

相关推荐

  • typescript - Define return type based on input values - Stack Overflow

    I'm trying to to connect to a 3rd party function, which will return an object typed as any, but it

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信