I want to create a function, that returns an object, and if the includeHtml or includeComponent options are passed to this function, the returned object includes a html and a component property respectfully.
My code works, but it's really hard to read, and feels off. Ideally I'd like to simply type an Options object, and use that, instead of having to use generics for each boolean property I want to use for the return type. Is that possible?
Here is the code (I also added a link to the TS REPL):
type DefaultReturn = {
default: true
}
export function loadAll<
TIncludeHtml extends boolean | undefined,
TIncludeComponent extends boolean | undefined,
>({
includeHtml = false,
includeComponent = false,
}: {
includeHtml?: TIncludeHtml
includeComponent?: TIncludeComponent
}):
DefaultReturn
& (TIncludeHtml extends true ? { html: string } : DefaultReturn)
& (TIncludeComponent extends true ? { component: string } : DefaultReturn) {
// I omitted the actual implementation.
return null as any;
}
const x = loadAll({
includeHtml: true,
includeComponent: true,
})