Type Narrowing Vapi.Call vs CallBatchResponse
# support
j
When I invoke
vapi.calls.create
it returns the following type:
Copy code
export type CallsCreateResponse = Vapi.Call | Vapi.CallBatchResponse;
Since there are no shared props, Typescript can't narrow it automatically. So I have to do something like:
Copy code
const isCall = (response: CallsCreateResponse): response is Vapi.Call => {
  return 'id' in response;
};
I’d rather avoid writing this manually. Am I missing a better way to handle this?
c
Use a type guard like
const isCall = (res: CallsCreateResponse): res is Vapi.Call => 'id' in res;
is concise and effective, especially given the lack of shared properties..
j
yea sure but i prefer to not even have to do this everytime I invoke this
k
Wrap calls.create in a helper function that handles type narrowing once, so your call sites stay clean and TypeScript infers types automatically. https://github.com/VapiAI/server-sdk-typescript
2 Views