Skip to content

Commit 7962862

Browse files
juliusmarmingeMaxLeiter
andauthoredMar 4, 2024··
fix(types): make sure Actions type is bound to prevent InferActions returning unknown (#1075)
Co-authored-by: Max Leiter <maxwell.leiter@gmail.com>
1 parent aa46723 commit 7962862

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed
 

‎.changeset/fresh-trees-listen.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': patch
3+
---
4+
5+
fix `useActions` type inference

‎examples/next-ai-rsc/app/page.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { toast } from '@/components/ui/use-toast';
2323

2424
export default function Page() {
2525
const [messages, setMessages] = useUIState<typeof AI>();
26-
const { submitUserMessage } = useActions();
26+
const { submitUserMessage } = useActions<typeof AI>();
2727
const [inputValue, setInputValue] = useState('');
2828
const { formRef, onKeyDown } = useEnterSubmit();
2929
const inputRef = useRef<HTMLTextAreaElement>(null);

‎packages/core/rsc/provider.tsx

+3-7
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,7 @@ export function createAI<
7777
? wrapAction(onGetUIState, {})
7878
: undefined;
7979

80-
async function AI(props: {
81-
children: React.ReactNode;
82-
initialAIState?: AIState;
83-
initialUIState?: UIState;
84-
}) {
80+
const AI: AIProvider<AIState, UIState, Actions> = async props => {
8581
if ('useState' in React) {
8682
// This file must be running on the React Server layer.
8783
// Ideally we should be using `import "server-only"` here but we can have a
@@ -114,7 +110,7 @@ export function createAI<
114110
{props.children}
115111
</InternalAIProvider>
116112
);
117-
}
113+
};
118114

119-
return AI as AIProvider<AIState, UIState, Actions>;
115+
return AI;
120116
}

‎packages/core/rsc/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export type AIProviderProps<AIState = any, UIState = any, Actions = any> = {
3131
children: React.ReactNode;
3232
initialAIState?: AIState;
3333
initialUIState?: UIState;
34+
/** $ActionTypes is only added for type inference and is never used at runtime **/
35+
$ActionTypes?: Actions;
3436
};
3537

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

0 commit comments

Comments
 (0)
Please sign in to comment.