Skip to content

Commit 7d5f1d8

Browse files
committed
fix: type constraint of Modifier type
1 parent 5915d3e commit 7d5f1d8

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

modifier.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,23 @@ import type { FlatType } from "./util/_typeutil.ts";
55
import { defineSource, type Source } from "./source.ts";
66
import { type Curator, defineCurator } from "./curator.ts";
77

8-
declare const UNSPECIFIED: unique symbol;
9-
type UNSPECIFIED = typeof UNSPECIFIED;
10-
118
export type ModifyParams<T> = {
129
readonly items: AsyncIterable<IdItem<T>>;
1310
};
1411

15-
export type Modifier<T, U = UNSPECIFIED> =
12+
export type Modifier<T, U = T> =
1613
& (<
1714
S extends Source<T> | Curator<T>,
1815
V extends S extends (Source<infer V> | Curator<infer V>) ? V : never,
19-
W extends U extends UNSPECIFIED ? V : U,
20-
R extends S extends Source<T> ? Source<FlatType<V & W>>
21-
: Curator<FlatType<V & W>>,
16+
R extends S extends Source<T> ? Source<FlatType<V & U>>
17+
: Curator<FlatType<V & U>>,
2218
>(source: S) => R)
2319
& {
24-
__phantom?: T;
20+
// This `__phantom` property is used for type constraint.
21+
__phantom?: (_: T) => void;
2522
};
2623

27-
export function defineModifier<T, U = UNSPECIFIED>(
24+
export function defineModifier<T, U = T>(
2825
modify: (
2926
denops: Denops,
3027
params: ModifyParams<T>,

modifier_test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,14 @@ Deno.test("defineModifier", async (t) => {
240240
},
241241
);
242242
assertEquals(typeof modifier, "function");
243-
assertType<IsExact<typeof modifier, Modifier<{ a: string }>>>(true);
243+
244+
// NOTE:
245+
// It seems `Modifier` is not comparable with `IsExact`.
246+
// So compare the result instead.
247+
const modified = modifier({} as Source<{ a: string }>);
248+
assertType<
249+
IsExact<typeof modified, Source<{ a: string }>>
250+
>(true);
244251
});
245252

246253
await t.step("without type constraint T and U", () => {
@@ -251,8 +258,12 @@ Deno.test("defineModifier", async (t) => {
251258
},
252259
);
253260
assertEquals(typeof modifier, "function");
261+
// NOTE:
262+
// It seems `Modifier` is not comparable with `IsExact`.
263+
// So compare the result instead.
264+
const modified = modifier({} as Source<{ a: string }>);
254265
assertType<
255-
IsExact<typeof modifier, Modifier<{ a: string }, { z: string }>>
266+
IsExact<typeof modified, Source<{ a: string; z: string }>>
256267
>(true);
257268
});
258269
});

0 commit comments

Comments
 (0)