Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix type #4

Merged
merged 3 commits into from
Nov 9, 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
22 changes: 22 additions & 0 deletions action_test.ts
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ Deno.test("Action", async (t) => {
const action: Action<{ a: string }> = {
invoke: () => {},
};

await t.step("passed type is equal to the type restriction", () => {
const items = action.invoke(
denops,
@@ -50,4 +51,25 @@ Deno.test("Action", async (t) => {
>
>(true);
});

await t.step(
"check if the type constraint correctly triggers the type checking",
() => {
const action1: Action<{ a: string }> = {
invoke: () => {},
};
const action2: Action<{ b: string }> = {
invoke: () => {},
};
const action3: Action<{ c: string }> = {
invoke: () => {},
};
function strictFunction<T extends { a: string }>(_: Action<T>) {}
strictFunction(action1);
// @ts-expect-error: 'a' is missing
strictFunction(action2);
// @ts-expect-error: 'a' is missing
strictFunction(action3);
},
);
});
2 changes: 2 additions & 0 deletions matcher.ts
Original file line number Diff line number Diff line change
@@ -19,6 +19,8 @@ export type MatchParams<T> = {
* Matcher that filters items based on user input.
*/
export type Matcher<T> = {
__phantom?: T; // This is required for type constraint.

/**
* Matches items against the provided query.
*
21 changes: 21 additions & 0 deletions matcher_test.ts
Original file line number Diff line number Diff line change
@@ -51,4 +51,25 @@ Deno.test("Matcher", async (t) => {
>
>(true);
});

await t.step(
"check if the type constraint correctly triggers the type checking",
() => {
const matcher1: Matcher<{ a: string }> = {
match: async function* () {},
};
const matcher2: Matcher<{ b: string }> = {
match: async function* () {},
};
const matcher3: Matcher<{ c: string }> = {
match: async function* () {},
};
function strictFunction<T extends { a: string }>(_: Matcher<T>) {}
strictFunction(matcher1);
// @ts-expect-error: 'a' is missing
strictFunction(matcher2);
// @ts-expect-error: 'a' is missing
strictFunction(matcher3);
},
);
});
21 changes: 21 additions & 0 deletions previewer_test.ts
Original file line number Diff line number Diff line change
@@ -52,4 +52,25 @@ Deno.test("Previewer", async (t) => {
>
>(true);
});

await t.step(
"check if the type constraint correctly triggers the type checking",
() => {
const previewer1: Previewer<{ a: string }> = {
preview: () => {},
};
const previewer2: Previewer<{ b: string }> = {
preview: () => {},
};
const previewer3: Previewer<{ c: string }> = {
preview: () => {},
};
function strictFunction<T extends { a: string }>(_: Previewer<T>) {}
strictFunction(previewer1);
// @ts-expect-error: 'a' is missing
strictFunction(previewer2);
// @ts-expect-error: 'a' is missing
strictFunction(previewer3);
},
);
});
21 changes: 21 additions & 0 deletions renderer_test.ts
Original file line number Diff line number Diff line change
@@ -51,4 +51,25 @@ Deno.test("Renderer", async (t) => {
>
>(true);
});

await t.step(
"check if the type constraint correctly triggers the type checking",
() => {
const renderer1: Renderer<{ a: string }> = {
render: () => {},
};
const renderer2: Renderer<{ b: string }> = {
render: () => {},
};
const renderer3: Renderer<{ c: string }> = {
render: () => {},
};
function strictFunction<T extends { a: string }>(_: Renderer<T>) {}
strictFunction(renderer1);
// @ts-expect-error: 'a' is missing
strictFunction(renderer2);
// @ts-expect-error: 'a' is missing
strictFunction(renderer3);
},
);
});
21 changes: 21 additions & 0 deletions sorter_test.ts
Original file line number Diff line number Diff line change
@@ -51,4 +51,25 @@ Deno.test("Sorter", async (t) => {
>
>(true);
});

await t.step(
"check if the type constraint correctly triggers the type checking",
() => {
const sorter1: Sorter<{ a: string }> = {
sort: () => {},
};
const sorter2: Sorter<{ b: string }> = {
sort: () => {},
};
const sorter3: Sorter<{ c: string }> = {
sort: () => {},
};
function strictFunction<T extends { a: string }>(_: Sorter<T>) {}
strictFunction(sorter1);
// @ts-expect-error: 'a' is missing
strictFunction(sorter2);
// @ts-expect-error: 'a' is missing
strictFunction(sorter3);
},
);
});