From 729e91cef7670f12f87d351024bda8a3f6eb17b1 Mon Sep 17 00:00:00 2001 From: Nikolay Bryskin Date: Fri, 3 Apr 2026 17:49:44 +0300 Subject: [PATCH 1/2] fix: accept readonly arrays in LogTopic type LogTopic now includes `readonly Hex[]` so that `as const` and readonly arrays are assignable to topic filters without type errors. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/types/contract.test-d.ts | 42 ++++++++++++++++++------------------ src/types/contract.ts | 4 ++-- src/types/misc.ts | 2 +- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/types/contract.test-d.ts b/src/types/contract.test-d.ts index 25e269d98b..bb9fd0a03a 100644 --- a/src/types/contract.test-d.ts +++ b/src/types/contract.test-d.ts @@ -215,10 +215,10 @@ test('GetEventArgs', () => { 'Transfer' > expectTypeOf().toEqualTypeOf< - `0x${string}` | `0x${string}`[] | null | undefined + `0x${string}` | readonly `0x${string}`[] | null | undefined >() expectTypeOf().toEqualTypeOf< - `0x${string}` | `0x${string}`[] | null | undefined + `0x${string}` | readonly `0x${string}`[] | null | undefined >() }) @@ -260,7 +260,7 @@ test('GetValue', () => { test('LogTopicType', () => { expectTypeOf>().toEqualTypeOf() - expectTypeOf>().toEqualTypeOf() + expectTypeOf>().toEqualTypeOf() expectTypeOf>().toEqualTypeOf() expectTypeOf>().toEqualTypeOf< @@ -271,7 +271,7 @@ test('LogTopicType', () => { test('AbiEventParameterToPrimitiveType', () => { expectTypeOf< AbiEventParameterToPrimitiveType<{ name: 'foo'; type: 'string' }> - >().toEqualTypeOf() + >().toEqualTypeOf() expectTypeOf< AbiEventParameterToPrimitiveType< { name: 'foo'; type: 'string' }, @@ -286,7 +286,7 @@ test('AbiEventTopicToPrimitiveType', () => { >().toEqualTypeOf<`0x${string}`>() expectTypeOf< AbiEventTopicToPrimitiveType<{ name: 'foo'; type: 'string' }, Hex[]> - >().toEqualTypeOf<`0x${string}`[][]>() // TODO: Is this correct? + >().toEqualTypeOf() // TODO: Is this correct? expectTypeOf< AbiEventTopicToPrimitiveType<{ name: 'foo'; type: 'string' }, null> >().toEqualTypeOf() @@ -300,7 +300,7 @@ test('AbiEventTopicToPrimitiveType', () => { >().toEqualTypeOf() expectTypeOf< AbiEventTopicToPrimitiveType<{ name: 'foo'; type: 'bool' }, Hex[]> - >().toEqualTypeOf() + >().toEqualTypeOf() }) test('AbiEventParametersToPrimitiveTypes', () => { @@ -310,7 +310,7 @@ test('AbiEventParametersToPrimitiveTypes', () => { [{ name: 'foo'; type: 'string'; indexed: true }] > >().toEqualTypeOf<{ - foo?: string | string[] | null | undefined + foo?: string | readonly string[] | null | undefined }>() expectTypeOf< AbiEventParametersToPrimitiveTypes< @@ -321,8 +321,8 @@ test('AbiEventParametersToPrimitiveTypes', () => { ] > >().toEqualTypeOf<{ - foo?: string | string[] | null | undefined - bar?: number | number[] | null | undefined + foo?: string | readonly string[] | null | undefined + bar?: number | readonly number[] | null | undefined }>() type Named_AllowNonIndexed = AbiEventParametersToPrimitiveTypes< @@ -338,9 +338,9 @@ test('AbiEventParametersToPrimitiveTypes', () => { } > expectTypeOf().toEqualTypeOf<{ - foo?: string | string[] | null | undefined - bar?: number | number[] | null | undefined - baz?: `0x${string}` | `0x${string}`[] | null | undefined + foo?: string | readonly string[] | null | undefined + bar?: number | readonly number[] | null | undefined + baz?: `0x${string}` | readonly `0x${string}`[] | null | undefined }>() type Named_DisableUnion = AbiEventParametersToPrimitiveTypes< [ @@ -373,8 +373,8 @@ test('AbiEventParametersToPrimitiveTypes', () => { > >().toEqualTypeOf< | readonly [] - | readonly [string | string[] | null] - | readonly [string | string[] | null, number | number[] | null] + | readonly [string | readonly string[] | null] + | readonly [string | readonly string[] | null, number | readonly number[] | null] >() type Unnamed_AllowNonIndexed = AbiEventParametersToPrimitiveTypes< @@ -391,12 +391,12 @@ test('AbiEventParametersToPrimitiveTypes', () => { > expectTypeOf().toEqualTypeOf< | readonly [] - | readonly [string | string[] | null] - | readonly [string | string[] | null, number | number[] | null] + | readonly [string | readonly string[] | null] + | readonly [string | readonly string[] | null, number | readonly number[] | null] | readonly [ - string | string[] | null, - number | number[] | null, - `0x${string}` | `0x${string}`[] | null, + string | readonly string[] | null, + number | readonly number[] | null, + `0x${string}` | readonly `0x${string}`[] | null, ] >() @@ -426,7 +426,7 @@ test('AbiEventParametersToPrimitiveTypes', () => { > >().toEqualTypeOf< | readonly [] - | readonly [string | string[] | null] - | readonly [string | string[] | null, number | number[] | null] + | readonly [string | readonly string[] | null] + | readonly [string | readonly string[] | null, number | readonly number[] | null] >() }) diff --git a/src/types/contract.ts b/src/types/contract.ts index 4cbbf9a854..0f95e42c6f 100644 --- a/src/types/contract.ts +++ b/src/types/contract.ts @@ -466,8 +466,8 @@ export type LogTopicType< topic extends LogTopic = LogTopic, > = topic extends Hex ? primitiveType - : topic extends Hex[] - ? primitiveType[] + : topic extends readonly Hex[] + ? readonly primitiveType[] : topic extends null ? null : never diff --git a/src/types/misc.ts b/src/types/misc.ts index 625ba085cc..fdffe13618 100644 --- a/src/types/misc.ts +++ b/src/types/misc.ts @@ -3,7 +3,7 @@ import type { OneOf } from './utils.js' export type ByteArray = Uint8Array export type Hex = `0x${string}` export type Hash = `0x${string}` -export type LogTopic = Hex | Hex[] | null +export type LogTopic = Hex | readonly Hex[] | null export type SignableMessage = | string | { From eb2eec6f1c9318ad7187bb0d3f9323dc32e12502 Mon Sep 17 00:00:00 2001 From: jxom <7336481+jxom@users.noreply.github.com> Date: Tue, 21 Apr 2026 11:44:17 +1000 Subject: [PATCH 2/2] chore: add changeset Amp-Thread-ID: https://ampcode.com/threads/T-019dad44-c9b7-73fd-9c97-2ffe6794479e --- .changeset/readonly-log-topic.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/readonly-log-topic.md diff --git a/.changeset/readonly-log-topic.md b/.changeset/readonly-log-topic.md new file mode 100644 index 0000000000..286980592b --- /dev/null +++ b/.changeset/readonly-log-topic.md @@ -0,0 +1,5 @@ +--- +"viem": patch +--- + +Fixed `LogTopic` type to accept readonly arrays.