hyperweb-io / hyperweb-io/telescope

Generated, compiled `Codec.typeUrl` is just TS type `string`

Open
#795 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
154
Forks
53
PR merge metrics
No merged PRs in 30d

Description

## Situation

Here's a difference from what Telescope generates today versus what I want from it:

```diff
diff --git a/src/codegen/circle/cctp/v1/attester.ts b/src/codegen/circle/cctp/v1/attester.ts
index 24d3c1b1dd..c5e728ae6a 100644
--- a/src/codegen/circle/cctp/v1/attester.ts
+++ b/src/codegen/circle/cctp/v1/attester.ts
@@ -26,7 +26,7 @@ function createBaseAttester(): Attester {
};
}
export const Attester = {
- typeUrl: '/circle.cctp.v1.Attester',
+ typeUrl: '/circle.cctp.v1.Attester' as const,
encode(
message: Attester,
writer: BinaryWriter = BinaryWriter.create(),
```

Without the `as const`, the Typescript compiler output is (today vs. what I want):

```diff
--- a/dist/codegen/circle/cctp/v1/attester.d.ts.orig 2025-07-11 20:52:11
+++ b/dist/codegen/circle/cctp/v1/attester.d.ts 2025-07-11 18:56:39
@@ -19,7 +19,7 @@
attester: string;
}
export declare const Attester: {
- typeUrl: string;
+ typeUrl: "/circle.cctp.v1.Attester";
encode(message: Attester, writer?: BinaryWriter): BinaryWriter;
decode(input: BinaryReader | Uint8Array, length?: number): Attester;
fromJSON(object: any): Attester;
```

## Impact

Because of that missing `as const`, an importer of the compiled files cannot use the narrow `typeUrl` constants because they're all just `string`. That's bad because it forces the user to rely on creating type registries rather than just grabbing `Codec['typeUrl']`.

## Solution

Turns out this is really easy to solve:

```diff
--- a/packages/ast/src/encoding/proto/props/index.ts
+++ b/packages/ast/src/encoding/proto/props/index.ts
@@ -3,6 +3,9 @@ import { ProtoParseContext } from '../../context';
import { ProtoType } from '@cosmology/types';
import { getAminoTypeName, getTypeUrl } from '../../amino';

+const asConst = (expr: t.Expression) =>
+ t.tSAsExpression(expr, t.tsTypeReference(t.identifier('const')));
+
export const createTypeUrlProperty = (
context: ProtoParseContext,
proto: ProtoType
@@ -11,7 +14,7 @@ export const createTypeUrlProperty = (
if (!typeUrl) return;
return t.objectProperty(
t.identifier('typeUrl'),
- t.stringLiteral(typeUrl)
+ asConst(t.stringLiteral(typeUrl))
)
};

@@ -23,6 +26,6 @@ export const createAminoTypeProperty = (
if (!str || str.startsWith('/')) return;
return t.objectProperty(
t.identifier('aminoType'),
- t.stringLiteral(str)
+ asConst(t.stringLiteral(str))
)
};
```

With this patch, I don't need anything else from you, but I thought I'd share it in case you wanted to adopt it upstream (with tests, of course).

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.