Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion src/functional/noop.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/**
* No operation function.
*/
export function noop() {}
export function noop() {
// no operation
}
46 changes: 46 additions & 0 deletions src/types/assert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
assertNotNullish,
assertUnreachable,
} from './assert.ts';
import type { Nullish } from './types.ts';

type Value = object | boolean | null | undefined;

Expand Down Expand Up @@ -55,6 +56,51 @@ describe('assert', () => {
});
});

describe('assert implicit', () => {
it('should assert object properties', () => {
interface Foo {
foo: string;
bar?: number | Nullish;
baz?: string | Nullish;
}

const fooOptional: Foo = {
foo: 'foo',
};

const fooRequired: Foo = {
foo: 'foo',
bar: 1,
baz: 'baz',
};

internalAssert(fooRequired.bar && fooRequired.baz);

expectTypeOf(fooRequired.bar).toEqualTypeOf<number>();
expectTypeOf(fooRequired.baz).toEqualTypeOf<string>();

expect(() =>
internalAssert(
fooOptional.bar && fooOptional.baz,
'bar and baz should be defined',
),
).toThrow('bar and baz should be defined');
});

it('should assert discriminated union', () => {
type Foo = { type: 'foo'; foo: string } | { type: 'bar'; bar: number };

const foo: Foo = { type: 'foo', foo: 'foo' };
const bar: Foo = { type: 'bar', bar: 1 };

internalAssert(foo.type === 'foo');
internalAssert(bar.type === 'bar');

expectTypeOf(foo).toEqualTypeOf<{ type: 'foo'; foo: string }>();
expectTypeOf(bar).toEqualTypeOf<{ type: 'bar'; bar: number }>();
});
});

describe('assertDefined', () => {
const map = new Map<string, string>([
['foo', 'bar'],
Expand Down
8 changes: 4 additions & 4 deletions src/types/assert.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { Falsy, Nullish } from './types.ts';
import type { Nullish } from './types.ts';

/**
* Basic assertion with (a possible lazy) message.
* Pay attention, it will throw an error if the value is falsy, even for 0, false, and empty strings.
* If these cases are not desired, use `assertDefined` or `assertDefinedNotNull` instead.
* @param value

Check warning on line 7 in src/types/assert.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Missing JSDoc @PARAM "value" description
* @param message

Check warning on line 8 in src/types/assert.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Missing JSDoc @PARAM "message" description
*/
export function assert<T>(
value: T,
export function assert(
value: unknown,
message: string | (() => string) = `value ${String(value)} is falsy`,
): asserts value is Exclude<T, Falsy> {
): asserts value {
if (!value) {
const finalMessage = typeof message === 'function' ? message() : message;
throw new Error(finalMessage);
Expand All @@ -20,7 +20,7 @@
/**
* Asserts that the given value is unreachable.
* Whenever possible, use ts-pattern instead.
* @param value

Check warning on line 23 in src/types/assert.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Missing JSDoc @PARAM "value" description
* @example switch-case
* ```ts
* type MyEnum = 'a' | 'b' | 'c';
Expand All @@ -45,7 +45,7 @@
* Useful to validate that value gotten from an array or record object is defined.
* Pay attention, it will throw an error only if the value is undefined.
* null will not throw an error. If you need it to throw an error for null, use `assertDefinedNotNull` instead.
* @param value

Check warning on line 48 in src/types/assert.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Missing JSDoc @PARAM "value" description
* @example get map item previously set
* ```ts
* const map = new Map<string, number>();
Expand All @@ -68,7 +68,7 @@

/**
* Ensures that the given value is not nullish.
* @param value

Check warning on line 71 in src/types/assert.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Missing JSDoc @PARAM "value" description
* @example React Ref
* ```ts
* function Component() {
Expand Down Expand Up @@ -98,8 +98,8 @@
}
}

/**

Check warning on line 101 in src/types/assert.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Missing JSDoc block description
* @param value

Check warning on line 102 in src/types/assert.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Missing JSDoc @PARAM "value" description
* @deprecated Use `assertNotNullish` instead.
*/
export function assertDefinedNotNull<T>(
Expand Down
Loading