I am encountering an issue with the inferred returnType of the following types. The problem lies in the cityAttractions
type, where it should ideally have a type error in its return statement for returning a string instead of an array of strings.
interface Tool<RESULT = any> {
returnType?: RESULT;
}
type ToolCallWithInferredReturnType<ToolsRecord extends Record<string, Tool>> = {
[ToolName in keyof ToolsRecord]: ToolsRecord[ToolName]['returnType']
}
export type OnToolCall<ToolsRecord extends Record<string, Tool> = Record<string, Tool>> =
(args: { toolCall: { toolName: string } }) => ToolsRecord extends Record<string, Tool>
? ToolCallWithInferredReturnType<ToolsRecord>[typeof args['toolCall']['toolName']]
: void | Promise<unknown> | unknown;
type Tools = {
weather: {
returnType: string;
}
cityAttractions: {
returnType: string[];
}
}
export const onToolCall: OnToolCall<Tools> = ({ toolCall }) => {
if (toolCall.toolName === 'weather') {
return 'Weather information was shown to the user.';
}
if (toolCall.toolName === 'cityAttractions') {
return "Attractions was shown to the user."; // type error should be throw here
}
throw Error("")
}