Skip to content

Commit

Permalink
fix(types): make sure Actions type is bound to prevent InferActions
Browse files Browse the repository at this point in the history
… returning unknown (#1075)

Co-authored-by: Max Leiter <[email protected]>
  • Loading branch information
juliusmarminge and MaxLeiter authored Mar 4, 2024
1 parent aa46723 commit 7962862
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-trees-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

fix `useActions` type inference
2 changes: 1 addition & 1 deletion examples/next-ai-rsc/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { toast } from '@/components/ui/use-toast';

export default function Page() {
const [messages, setMessages] = useUIState<typeof AI>();
const { submitUserMessage } = useActions();
const { submitUserMessage } = useActions<typeof AI>();
const [inputValue, setInputValue] = useState('');
const { formRef, onKeyDown } = useEnterSubmit();
const inputRef = useRef<HTMLTextAreaElement>(null);
Expand Down
10 changes: 3 additions & 7 deletions packages/core/rsc/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,7 @@ export function createAI<
? wrapAction(onGetUIState, {})
: undefined;

async function AI(props: {
children: React.ReactNode;
initialAIState?: AIState;
initialUIState?: UIState;
}) {
const AI: AIProvider<AIState, UIState, Actions> = async props => {
if ('useState' in React) {
// This file must be running on the React Server layer.
// Ideally we should be using `import "server-only"` here but we can have a
Expand Down Expand Up @@ -114,7 +110,7 @@ export function createAI<
{props.children}
</InternalAIProvider>
);
}
};

return AI as AIProvider<AIState, UIState, Actions>;
return AI;
}
2 changes: 2 additions & 0 deletions packages/core/rsc/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export type AIProviderProps<AIState = any, UIState = any, Actions = any> = {
children: React.ReactNode;
initialAIState?: AIState;
initialUIState?: UIState;
/** $ActionTypes is only added for type inference and is never used at runtime **/
$ActionTypes?: Actions;
};

export type AIProvider<AIState = any, UIState = any, Actions = any> = (
Expand Down

0 comments on commit 7962862

Please sign in to comment.