Closed valyagolev closed 7 months ago
@valyagolev Hope I'm not answering too late.
I already had this issue in other libs. This is some TS internals dark magic that I don't understand anything about, but it will work if you use a second unconstrained generic like this:
export type GPTFunction = {
name: string;
description?: string;
parameters: { type: "object" } & JSONSchema;
};
export type Return<T extends GPTFunction> = FromSchema<T["parameters"]>;
export const SetImprovedDescription = {
name: "set_improved_description",
description: "Set improved description",
parameters: {
type: "object",
properties: {
description: { type: "string" },
explanation_for_changes: { type: "string" },
},
required: ["description"],
additionalProperties: false,
},
} as const satisfies GPTFunction;
// Will work !
function call_gpt<T extends GPTFunction, R = Return<T>>(
schema: T,
system: string,
user: string,
): null | R {
return null;
}
const a = call_gpt(SetImprovedDescription, "system", "user");
Will add a section in the FAQ about that!
I was trying to come up with a way to describe GPT functions, along with the name and description:
But then this doesn't work:
It errors out with:
Most weirdly, while it does error out, it actually still correctly infers the type!
I also tried simplifying the function declaration. Avoiding my types (Return and GPTFunction), and replacing them with their applications directly doesn't change the error.
This works, but it's not what I need unfortunately:
Another attempt that doesn't work, and it's actually quite a simple thing:
No custom types, very simple generic... still I get, even without ever calling this, for "return":
What do I miss? Did I misconfigure something, or is there a different ways to use generics here?