Skip to content
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

Use as const in TypeScript generator #357

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Core/Generators/TypeScript/TypeScriptGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -737,14 +737,13 @@ public override ValueTask<string> Compile(BebopSchema schema, GeneratorConfig co
{
if (td.OpcodeDecorator is not null && td.OpcodeDecorator.TryGetValue("fourcc", out var fourcc))
{
builder.AppendLine($"public static readonly opcode: number = {fourcc} as {fourcc};");
builder.AppendLine($"public static readonly opcode = {fourcc} as const;");
}
if (td.DiscriminatorInParent != null)
{
// We codegen "1 as 1", "2 as 2"... because TypeScript otherwise infers the type "number" for this field, whereas a literal type is necessary to discriminate unions.
builder.AppendLine($"public readonly discriminator: number = {td.DiscriminatorInParent} as {td.DiscriminatorInParent};");
builder.AppendLine($"public readonly discriminator = {td.DiscriminatorInParent} as const;");
// back compaq for v2 usage where discriminator is a static field
builder.AppendLine($"public static readonly discriminator: number = {td.DiscriminatorInParent} as {td.DiscriminatorInParent};");
builder.AppendLine($"public static readonly discriminator = {td.DiscriminatorInParent} as const;");
}
for (var i = 0; i < fd.Fields.Count; i++)
{
Expand Down
Loading