ai
ai copied to clipboard
useAIState with key not correctly inferring type
Description
useAIState with a key does not return the proper type
const [chatId] = useAIState<typeof AI>('chatId');
In this case chatId has a union type of any of the the properties of AIState.
The only workaround is to cast as unknown and type
const [chatId] = useAIState<typeof AI>('chatId') as unknown as [string];
Code example
When using useAIState with a key, the proper value is returned but the types are misunderstood. For the below example:
// actions.tsx
export interface AIState {
isLoading: boolean;
messages: {
id: string;
role: MessageRoles;
content: string;
}[];
chatId: string;
}
const AI = export const AI = createAI<AIState, UIState[]>({
//...
})
// client component
const [chatId] = useAiState<typeof AI>('chatId)`;
// basic string operation:
chatId.slice(0,1);
Gives the following error:
Property 'slice' does not exist on type 'string | boolean | { id: string; role: MessageRoles; content: string; }[]'. Property 'slice' does not exist on type 'false'
Additional context
No response