Reputation: 1763
Im trying to define some types that say that a callback must be used in one of two ways, either it must be an error in which case it should not have a result, or it must be a result in which case it must not have an error.
So as an example here is a method where im trying to use it:
import { LambdaCallback } from "./types";
export function handler(event:any, context:any, callback:LambdaCallback) {
console.log(event)
callback(null, {
statusCode: 200,
body: JSON.stringify({msg: "Hello, World!!"})
})
}
This is my attempt at the types:
export type LambdaCallbackError = string | {};
export type LambdaCallResult = {
statusCode: number,
body: string
}
export type LambdaCallbackWithError = (error:LambdaCallbackError) => void;
export type LambdaCallbackWithResult = (error:null, result:LambdaCallResult) => void;
export type LambdaCallback = LambdaCallbackWithError | LambdaCallbackWithResult;
The types compile fine but im getting the following error when trying to use it:
lambda/hello.ts(6,5): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'LambdaCallback' has no compatible call signatures.
Any ideas?
Upvotes: 2
Views: 142
Reputation: 276085
Your code LambdaCallbackWithError | LambdaCallbackWithResult
This is wrong. You do not have a single function that can be one of those. You have a single function that is both of those.
You should:
export type LambdaCallback = (error:null | LambdaCallbackError, result:LambdaCallResult) => void;
Upvotes: 4