Skip to main content

Hook Data

The HookData type represents the non-null data from a BAML React hook. Useful when you know data exists and want to avoid undefined checks.

Usage​

function Component() {
const hook = useTestAws({ stream: true });
const data = hook.data;

return (
<div>
{data} {/* No need for null checks */}
</div>
);
}

Type Definition​

type HookData<
FunctionName extends FunctionNames,
Options extends { stream?: boolean } = { stream?: true }
> = NonNullable<HookOutput<FunctionName, Options>['data']>

Type Parameters​

ParameterTypeDescription
FunctionNameFunctionNamesBAML function name, used to infer types
Options{ stream?: boolean }Streaming behavior config. Defaults to { stream?: true }

Details​

HookData is a utility type that removes undefined from the data property of HookOutput. The resulting type is either FinalDataType or StreamDataType depending on the streaming configuration.

Examples​

Streaming Data​

// For streaming hooks, data can be either stream or final data
const streamingData: HookData<'TestAws', { stream: true }> = "Streaming response...";

Non-Streaming Data​

// For non-streaming hooks, data is always final data
const nonStreamingData: HookData<'TestAws', { stream: false }> = "Final response";

Type Guard Usage​

function renderData(data: HookData<'WriteStory', { stream: true }>) {
// data is guaranteed to be non-null here
return <div>{data.content}</div>;
}

See Also​