本页还没有译文
本页 11 段还没译成日本語,下面显示的是英文原文。Type Error with onToolCall
还没翻译译自英文(ai-sdk.dev)本页 11 段都还没译
When using the onToolCall callback with TypeScript, you may encounter type errors when trying to pass tool properties directly to addToolOutput.
Problem
TypeScript cannot automatically narrow the type of toolCall.toolName when you have both static and dynamic tools, leading to type errors:
// ❌ This causes a TypeScript error
const { messages, sendMessage, addToolOutput } = useChat({
async onToolCall({ toolCall }) {
addToolOutput({
tool: toolCall.toolName, // Type 'string' is not assignable to type '"yourTool" | "yourOtherTool"'
toolCallId: toolCall.toolCallId,
output: someOutput,
});
},
});The error occurs because:
- Static tools have specific literal types for their names (e.g.,
"getWeatherInformation") - Dynamic tools have
toolNameas a genericstring - TypeScript can't guarantee that
toolCall.toolNamematches your specific tool names
Solution
Check if the tool is dynamic first to enable proper type narrowing:
// ✅ Correct approach with type narrowing
const { messages, sendMessage, addToolOutput } = useChat({
async onToolCall({ toolCall }) {
// Check if it's a dynamic tool first
if (toolCall.dynamic) {
return;
}
// Now TypeScript knows this is a static tool with the correct type
addToolOutput({
tool: toolCall.toolName, // No type error!
toolCallId: toolCall.toolCallId,
output: someOutput,
});
},
});Note
If you're still using the deprecated addToolResult method, this solution
applies the same way. Consider migrating to addToolOutput for consistency
with the latest API.
Related
AI 翻译查看英文原文