Skip to content

Type collision when combining outputTypeAnnotations=optional and outputTypeRegistry=true #1216

@gagiv

Description

@gagiv

Environment:
ts-proto: 2.7.5
typescript: 5.8.3

Problem:
When generating code with both:

outputTypeRegistry=true

outputTypeAnnotations=optional

Each generated message interface has $type marked as optional due to outputTypeAnnotations=optional:

export const Foo = {
  $type?: "demo.Foo"
}

UnknownMessage in typeRegistry.ts is defined as:

export type UnknownMessage = { $type: string }

When i try to register the message:

messageTypeRegistry.set(Foo.$type, Foo)

Typescript complains that:

Argument of type 'typeof Foo' is not assignable to parameter of type 'UnknownMessage'.
Types of property '$type' are incompatible.

This happens because Foo.$type is optional, while UnknownMessage.$type is required.

Expected Behavior:

We still want $type to always exist for registry purposes, but we don’t want user code to be forced to pass it when constructing messages. One possible solution is to
generate $type as a readonly literal property that is set internally by the generator, not by user code.
or, separate the “user-facing” message interface from the “registry-facing” type, where $type is required only in the registry-facing one.

Best regards.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions