Skip to content

Commit

Permalink
feat: finish Numeric type
Browse files Browse the repository at this point in the history
  • Loading branch information
ashgw committed Apr 17, 2024
1 parent 168b5e3 commit 487ef42
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
6 changes: 1 addition & 5 deletions CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ yarn add ts-extended

### Example
```ts
import { locked, final } from 'ts-extended';
import type {
Maybe,
Primitive,
Newable,
Callable
} from 'ts-extended';
import { locked, final } from 'ts-extended';

export type F<A extends Primitive, R extends Newable> = Callable<A[], R>;

Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
*/
export type Nullable = null | undefined;

/**
* Represents a type that can hold any numeric value: number or a bigint.
* @type {Numeric}
*/
export type Numeric = number | bigint;

export type Primitive = Nullable | Numeric | string | boolean | symbol;
export type Falsy = false | '' | 0 | Nullable;
export type IsFalsy<T> = T extends Falsy ? true : false;
Expand Down
5 changes: 2 additions & 3 deletions tests/nullable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { Nullable } from 'src';
import { describe, assertType, expect, expectTypeOf, test, it } from 'vitest';

test.fails('fail test', () => {
type _T = { foo: boolean };
// @ts-expect-error, it cannot be _T, it should error out
expect(assertType<_T>(null)).rejects.toBe(true);
// @ts-expect-error, it cannot be this type, it should error out
expect(assertType<Nullable>({ foo: boolean })).rejects.toBe(true);
});

test('concrete types', () => {
Expand Down
21 changes: 21 additions & 0 deletions tests/numeric.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Numeric } from 'src';
import { describe, assertType, expect, expectTypeOf, test, it } from 'vitest';

test.fails('fail test', () => {
// @ts-expect-error, it cannot be null, it should error out
expect(assertType<Numeric>(null)).rejects.toBe(true);
});

test('concrete types', () => {
expectTypeOf(21).toMatchTypeOf<Numeric>();
expectTypeOf(9007199254099).toMatchTypeOf<Numeric>();
expectTypeOf(BigInt(69)).toMatchTypeOf<Numeric>();
expectTypeOf(Number(69)).toMatchTypeOf<Numeric>();
});

describe('_', () => {
it('should be defined', () => {
const _: Numeric = 1;
expect(_).toBeDefined();
});
});

0 comments on commit 487ef42

Please sign in to comment.