Skip to content

Commit

Permalink
feat: finish Nullable type
Browse files Browse the repository at this point in the history
  • Loading branch information
AshGw committed Apr 17, 2024
1 parent e4349cc commit 3607191
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 36 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@
Bullet proof TypeScript even more
### Installation
```bash
# npm
npm i ts-extended

# pnpm
pnpm i ts-extended

# yarn
yarn add ts-extended
```

### Example
```ts
import {
locked,
final,
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
@@ -1,4 +1,9 @@
/**
* `Nullable` type allows representing a value that can be either null or undefined.
* @type {Nullable}
*/
export type Nullable = null | undefined;

export type Numeric = number | bigint;
export type Primitive = Nullable | Numeric | string | boolean | symbol;
export type Falsy = false | '' | 0 | Nullable;
Expand Down
14 changes: 0 additions & 14 deletions tests/index.test.ts

This file was deleted.

34 changes: 34 additions & 0 deletions tests/nullable.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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 a _T, it should error out
expect(assertType<_T>(null)).rejects.toBe(true);
});

test('concrete types', () => {
expectTypeOf(null).toMatchTypeOf<Nullable>();
expectTypeOf(undefined).toMatchTypeOf<Nullable>();
});

describe('_', () => {
it('should accept null', () => {
const value: Nullable = null;
expect(value).toBeNull();
});
it('should accept undefined', () => {
const value: Nullable = undefined;
expect(value).toBeUndefined();
});

it('should handle type unions with Nullable', () => {
type _Union = string | Nullable;
const value1: _Union = 'foo';
expect(value1).toBe('foo');
const value2: _Union = null;
expect(value2).toBeNull();
const value3: _Union = undefined;
expect(value3).toBeUndefined();
});
});
21 changes: 2 additions & 19 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
import tsconfigPaths from 'vite-tsconfig-paths';
import { defineConfig } from 'vitest/config';
import { MaybeUndefined } from '@/index';

function _getBoolean(enVar: MaybeUndefined<string>): boolean {
if (enVar === undefined) {
return false;
}
const asNumber = Number(enVar);
return Number.isNaN(asNumber)
? Boolean(String(enVar).toLowerCase().replace('false', ''))
: Boolean(asNumber);
}

const _useCompiledTests = _getBoolean(process.env['USE_COMPILED_TESTS']);

const _testFilePattern = `${
_useCompiledTests ? './tests-compiled' : '.'
}/**/*.test.${_useCompiledTests ? 'js' : 'ts'}`;

export default defineConfig({
plugins: [tsconfigPaths()],
test: {
include: [_testFilePattern],
includeSource: ['src/**/[!index]*.ts'],
coverage: {
reporter: ['lcov', 'text', 'json', 'html'],
all: true,
include: ['src/**/*'],
reporter: ['lcov', 'text'],
watermarks: {
lines: [80, 95],
functions: [80, 95],
Expand Down

0 comments on commit 3607191

Please sign in to comment.