JesusTheHun
JesusTheHun
Aarktype
Created by JesusTheHun on 10/29/2024 in #questions
Union of generics
I've tried to write the following :
export const arkQueryCriteriaValue = type('<Value>', [
{ eq: 'Value' },
'|',
{ notEq: 'Value' },
'|',
{ in: 'Value[]' },
]);
export const arkQueryCriteriaValue = type('<Value>', [
{ eq: 'Value' },
'|',
{ notEq: 'Value' },
'|',
{ in: 'Value[]' },
]);
But unfortunately it doesn't work because the 2nd parameter accepts a maximum of 3 elements. Is there a way to add arbitrary number of element to a union issued from a generic ?
23 replies
Aarktype
Created by JesusTheHun on 10/29/2024 in #questions
Merging types instead of using an intersection when using generics
I have a generic type like this one :
generic('T', 'M')([ 'T', '&', { metadata: { id: 'M' }} ])
generic('T', 'M')([ 'T', '&', { metadata: { id: 'M' }} ])
If the first argument already contains the property metadata, I would like this property to be dropped and replaced by the one from my generic type, instead of having an intersection, like in my example. Is it possible to do something like that ?
7 replies
Aarktype
Created by JesusTheHun on 10/28/2024 in #questions
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"
);
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 } }
>;
}
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
21 replies