-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PartialMessage<T> of message with oneof field requires that oneof field be non-partial #380
Comments
Good call. Would you like to open a PR against the 3.x branch? |
I believe I have a solution that can work for the existing v2 representation. I know this type SetOneof = { oneofKind: string };
type SetOneofKind<T extends SetOneof> = T extends { oneofKind: string & keyof T }
? T['oneofKind']
: never;
type PartialOneofValueMap<T extends SetOneof, K extends SetOneofKind<T> = SetOneofKind<T>> = {
[k in K]: { oneofKind: k } & {
[p in k]: T extends { oneofKind: k } ? PartialField<T[k]> : never;
};
};
type PartialOneof<
T extends SetOneof,
K extends SetOneofKind<T> = SetOneofKind<T>,
M extends PartialOneofValueMap<T, K> = PartialOneofValueMap<T, K>
> = T extends { oneofKind: keyof M } ? M[keyof M] : { oneofKind: undefined };
export type PartialMessage<T extends object> = {
[K in keyof T]?: PartialField<T[K]>
}; type PartialField<T> =
T extends (Date | Uint8Array | bigint | boolean | string | number) ? T
: T extends Array<infer U> ? Array<PartialField<U>>
: T extends ReadonlyArray<infer U> ? ReadonlyArray<PartialField<U>>
: T extends { oneofKind: string & keyof T } ? PartialOneof<T>
: T extends { oneofKind: undefined } ? T
: T extends object ? PartialMessage<T>
: T ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Calling
.create()
on aMessageType
instance allows partial oneof fields at runtime, but at compile-time they are required to be non-partial. TS PlaygroundI don't think there's actually a way to get this working given the existing types. This is another instance where the updated oneof representation would make this trivial:
: T extends { kind: string; value: infer U } ? { kind: T['kind']; value: PartialField<U> }
.The text was updated successfully, but these errors were encountered: