Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useAIState with key not correctly inferring type #1459

Open
mrjasonroy opened this issue Apr 27, 2024 · 0 comments
Open

useAIState with key not correctly inferring type #1459

mrjasonroy opened this issue Apr 27, 2024 · 0 comments
Labels

Comments

@mrjasonroy
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants