Imagine this scenario with a factory function
const createIndex = <PK extends string, SK extends string>(pk: PK, sk?: SK) => ({ pk, sk });
const i1 = createIndex("pk1"); // expected type of i1: { pk: "pk1" }
const i2 = createIndex("pk1", "sk1"); // expected type of i2: { pk: "pk1", sk: "sk1" }
I want to enhance this functionality so that the sk
parameter becomes optional, and based on its presence, the return type will either be { pk: PK }
or { sk: SK }
The challenge here lies in handling the optional aspect.