Skip to content

Variant names clash in TypeScript #926

@yannbolliger

Description

@yannbolliger

We have some WITs:

variant operation {
  create(operation-create),
  edit(operation-edit)
}
record operation-create {
  credit: u64,
  metadata: metadata, // some other record
}
record operation-edit {
  credit: u64
}

This generates the following TypeScript:

export type Operation = OperationCreate | OperationEdit;
export interface OperationCreate {
  tag: 'create',
  val: OperationCreate,
}
export interface OperationEdit {
  tag: 'edit',
  val: OperationEdit,
}
export interface OperationCreate {
  credit: bigint,
  metadata: Metadata,
}
export interface OperationEdit {
  credit: bigint,
}

⚡️ the problem is that two interfaces now alias each other. This makes TypeScript merge the declarations and we have a recursive type. 🤯

In general, if a variant has a record field that has as name the concatenation of the variant/enum and the variant branch, the generated TypeScript will contain clashing interface declarations.

Could the generator maybe check if the name is not already present/generated to avoid this?

(We generate these WIT from Rust enums. Therefore, enum variants with named fields are extracted to records that concat the name of the enum with the name of the variant. Of course, we can quick-fix this by changing our generator but the issue is still relevant.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions