Skip to content

Commit 5a1de8d

Browse files
committed
🛡️ Better safeguarding in the type predicate.
1 parent 19d8962 commit 5a1de8d

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/task-queue-descriptor.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ export const TaskQueueDescriptor = <I, O>(
1616
) => descriptor;
1717

1818
export const isTaskQueueDescriptor = (
19-
value: object
20-
): value is TaskQueueDescriptor<unknown, unknown> => {
21-
const candidate = value as TaskQueueDescriptor<unknown, unknown>;
19+
candidate: any
20+
): candidate is TaskQueueDescriptor<unknown, unknown> => {
21+
if (typeof candidate !== "object") return false;
22+
if (candidate === null) return false;
2223
if (typeof candidate.name !== 'string') return false;
2324
if (!isCodec(candidate.codec)) return false;
2425
if (typeof candidate.task !== 'function') return false;

test/unit/task-queue-descriptor.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ describe('TaskQueueDescriptor', () => {
1616
});
1717

1818
describe('.isTaskQueueDescriptor()', () => {
19+
it.each([
20+
[1],
21+
[true],
22+
[false],
23+
[null],
24+
[undefined],
25+
[{}],
26+
[[]],
27+
[() => {}],
28+
])("should return false if the candidate is not a non-null object", candidate => {
29+
expect(isTaskQueueDescriptor(candidate)).toBe(false);
30+
});
31+
1932
it('should return true if the value is a TaskQueueDescriptor', () => {
2033
buildStubRecords({
2134
name: ['test'],

0 commit comments

Comments
 (0)