Replies: 1 comment
-
Well, after I wrote this all up, I decided to let AI have a crack and it gave me this: export function TypeReplicateArray<T extends TSchema>(
type: T,
annotations?: SchemaOptions
): TUnion<[T, TArray<T>]> & { replicate: true } {
return Type.Union([type, Type.Array(type)], {
...(annotations as object),
replicate: true,
}) as any;
}
type UnwrapArrayUnion<T> = T extends Array<infer U> | infer U ? U : T;
type Properties<S extends TObject<any>> = S extends TObject<infer P> ? P : never;
export type DeReplicateStatic<S extends TObject<any>> = {
[K in keyof Properties<S>]: Properties<S>[K] extends { replicate: true }
? UnwrapArrayUnion<Static<Properties<S>[K]>>
: Static<Properties<S>[K]>;
}; Which does indeed give me the TypeExample and TypeExample2 types I want. Any thoughts? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a utility
TypeReplicateArray
that generates a union of a type and an array of that type (so string => string | string[] etc). With the keywordreplicate
in there so at runtime I can do something special. This all works fine.So
And the type of TypeExample is
This is fine. But I want a version where I simplify the ones marked replicate from string|string[] back to just string
gives me
But I am having a hard figuring out how to do it just for the one using TypeReplicateArray (marking with replicate:true). What I want is:
Sometimes I want
name: string
and sometimes I wantname: string | string[]
. I have not been able to find a way.Should I register a new type with [Kind]: "ReplicateOptionalArray" or something? I would like to find a "TypeBox" way.
Beta Was this translation helpful? Give feedback.
All reactions