diff --git a/README.md b/README.md index 661c147b..7b285579 100644 --- a/README.md +++ b/README.md @@ -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 = Callable; diff --git a/src/index.ts b/src/index.ts index e9deb66c..d670c01b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; diff --git a/tests/index.test.ts b/tests/index.test.ts deleted file mode 100644 index 411cc9f9..00000000 --- a/tests/index.test.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { expect, describe, it } from 'vitest'; -import { Nullable } from '@/index'; - -describe('Nullable type', () => { - it('should accept null', () => { - const value: Nullable = null; - expect(value).toBeNull(); - }); - - it('should accept undefined', () => { - const value: Nullable = undefined; - expect(value).toBeUndefined(); - }); -}); diff --git a/tests/nullable.test.ts b/tests/nullable.test.ts new file mode 100644 index 00000000..f23ce79b --- /dev/null +++ b/tests/nullable.test.ts @@ -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(); + expectTypeOf(undefined).toMatchTypeOf(); +}); + +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(); + }); +}); diff --git a/vitest.config.ts b/vitest.config.ts index aca29436..e9d7d5a9 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,31 +1,14 @@ import tsconfigPaths from 'vite-tsconfig-paths'; import { defineConfig } from 'vitest/config'; -import { MaybeUndefined } from '@/index'; - -function _getBoolean(enVar: MaybeUndefined): 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],