Skip to content

Commit 5e0a6f3

Browse files
authored
Merge pull request #4 from vim-fall/fix-type
Fix type
2 parents 781c16a + 396d5ed commit 5e0a6f3

File tree

6 files changed

+108
-0
lines changed

6 files changed

+108
-0
lines changed

action_test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Deno.test("Action", async (t) => {
88
const action: Action<{ a: string }> = {
99
invoke: () => {},
1010
};
11+
1112
await t.step("passed type is equal to the type restriction", () => {
1213
const items = action.invoke(
1314
denops,
@@ -50,4 +51,25 @@ Deno.test("Action", async (t) => {
5051
>
5152
>(true);
5253
});
54+
55+
await t.step(
56+
"check if the type constraint correctly triggers the type checking",
57+
() => {
58+
const action1: Action<{ a: string }> = {
59+
invoke: () => {},
60+
};
61+
const action2: Action<{ b: string }> = {
62+
invoke: () => {},
63+
};
64+
const action3: Action<{ c: string }> = {
65+
invoke: () => {},
66+
};
67+
function strictFunction<T extends { a: string }>(_: Action<T>) {}
68+
strictFunction(action1);
69+
// @ts-expect-error: 'a' is missing
70+
strictFunction(action2);
71+
// @ts-expect-error: 'a' is missing
72+
strictFunction(action3);
73+
},
74+
);
5375
});

matcher.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export type MatchParams<T> = {
1919
* Matcher that filters items based on user input.
2020
*/
2121
export type Matcher<T> = {
22+
__phantom?: T; // This is required for type constraint.
23+
2224
/**
2325
* Matches items against the provided query.
2426
*

matcher_test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,25 @@ Deno.test("Matcher", async (t) => {
5151
>
5252
>(true);
5353
});
54+
55+
await t.step(
56+
"check if the type constraint correctly triggers the type checking",
57+
() => {
58+
const matcher1: Matcher<{ a: string }> = {
59+
match: async function* () {},
60+
};
61+
const matcher2: Matcher<{ b: string }> = {
62+
match: async function* () {},
63+
};
64+
const matcher3: Matcher<{ c: string }> = {
65+
match: async function* () {},
66+
};
67+
function strictFunction<T extends { a: string }>(_: Matcher<T>) {}
68+
strictFunction(matcher1);
69+
// @ts-expect-error: 'a' is missing
70+
strictFunction(matcher2);
71+
// @ts-expect-error: 'a' is missing
72+
strictFunction(matcher3);
73+
},
74+
);
5475
});

previewer_test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,25 @@ Deno.test("Previewer", async (t) => {
5252
>
5353
>(true);
5454
});
55+
56+
await t.step(
57+
"check if the type constraint correctly triggers the type checking",
58+
() => {
59+
const previewer1: Previewer<{ a: string }> = {
60+
preview: () => {},
61+
};
62+
const previewer2: Previewer<{ b: string }> = {
63+
preview: () => {},
64+
};
65+
const previewer3: Previewer<{ c: string }> = {
66+
preview: () => {},
67+
};
68+
function strictFunction<T extends { a: string }>(_: Previewer<T>) {}
69+
strictFunction(previewer1);
70+
// @ts-expect-error: 'a' is missing
71+
strictFunction(previewer2);
72+
// @ts-expect-error: 'a' is missing
73+
strictFunction(previewer3);
74+
},
75+
);
5576
});

renderer_test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,25 @@ Deno.test("Renderer", async (t) => {
5151
>
5252
>(true);
5353
});
54+
55+
await t.step(
56+
"check if the type constraint correctly triggers the type checking",
57+
() => {
58+
const renderer1: Renderer<{ a: string }> = {
59+
render: () => {},
60+
};
61+
const renderer2: Renderer<{ b: string }> = {
62+
render: () => {},
63+
};
64+
const renderer3: Renderer<{ c: string }> = {
65+
render: () => {},
66+
};
67+
function strictFunction<T extends { a: string }>(_: Renderer<T>) {}
68+
strictFunction(renderer1);
69+
// @ts-expect-error: 'a' is missing
70+
strictFunction(renderer2);
71+
// @ts-expect-error: 'a' is missing
72+
strictFunction(renderer3);
73+
},
74+
);
5475
});

sorter_test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,25 @@ Deno.test("Sorter", async (t) => {
5151
>
5252
>(true);
5353
});
54+
55+
await t.step(
56+
"check if the type constraint correctly triggers the type checking",
57+
() => {
58+
const sorter1: Sorter<{ a: string }> = {
59+
sort: () => {},
60+
};
61+
const sorter2: Sorter<{ b: string }> = {
62+
sort: () => {},
63+
};
64+
const sorter3: Sorter<{ c: string }> = {
65+
sort: () => {},
66+
};
67+
function strictFunction<T extends { a: string }>(_: Sorter<T>) {}
68+
strictFunction(sorter1);
69+
// @ts-expect-error: 'a' is missing
70+
strictFunction(sorter2);
71+
// @ts-expect-error: 'a' is missing
72+
strictFunction(sorter3);
73+
},
74+
);
5475
});

0 commit comments

Comments
 (0)