arktypea
arktype16mo ago
20 replies
JesusTheHun

Generic & intersection

I'm looking to have a helper
arkWithMetadata
, which basically takes a base type and add
{ metadata: { id: <id> }}

I wanted to write this :
export const arkWithMetadata = type(
  '<doc extends Record<string, unknown>, id extends string>',
  "{ metadata: { id: 'id' } & doc"
);


But it doesn't work.
I want to retain both types
doc
and
id
.
Is there a way to do that ?

I also tried to write a function :

export function arkWithMetadata<
  const Doc extends Record<string, unknown>,
  const Id extends string,
>(doc: Type<Doc>, id: Type<Id>) {
  return type(doc as any).and(id as any) as Type<
    Doc & { metadata: { id: Id; cas: string } }
  >;
}


But it doesn't work either 🤷‍♂️
PS: I'm using
v2.0.0-rc.18
Was this page helpful?