typeguards

Allowing multiple differently shaped interfaces as TypeScript return types

守給你的承諾、 提交于 2021-02-05 06:43:25
问题 I have a function that takes a few parameters and generates objects that will be passed into an external process. Sine I have no control over the shapes that need to be ultimately created, I have to be able to take some varying parameters to my function and assemble them into the appropriate objects. Here's a really basic example that exhibits the issue I'm having: interface T1A { type: 'type1'; index: number; } interface T1B { type: 'type1'; name: string; } interface T2A { type: 'type2';