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
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"import/no-extraneous-dependencies": [
"error",
{
"devDependencies": ["./bin/*", "./vitest.config.mts", "./lint-staged.config.mjs", "**/*.{bench,test}.ts"]
"devDependencies": ["./bin/*", "./vitest.config.mts", "./lint-staged.config.mjs", "**/*.{bench,test,test-d}.ts"]
}
],
"import/no-relative-packages": "error",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@
"typecheck": "tsc --build --verbose --noEmit",
"lint": "eslint ./*.{js,cjs,mjs,ts,cts,mts} ./src/ ./bench/ ./test/ --ext .ts,.mjs,.cjs",
"bench": "vitest bench",
"test": "vitest",
"coverage": "vitest run --coverage",
"test": "vitest --typecheck",
"coverage": "vitest run --coverage --typecheck",
"validate": "validate-package-exports --check --verify",
"spellcheck": "cspell './{.github,src,bench,test}/**/*.{ts,json}' './*.{md,mjs,mts}' './package.json' --no-progress",
"prepublishOnly": "pnpm typecheck && pnpm spellcheck && pnpm lint && pnpm coverage && pnpm build",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

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

93 changes: 93 additions & 0 deletions test/predicate-factory/createObjectShapePredicate.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { describe, expectTypeOf, it } from 'vitest';

import { createNumberRangePredicate, isNumber } from '../../src/index.js';
import { isOptionalString } from '../../src/predicate/isOptionalString.js';
import { isString } from '../../src/predicate/isString.js';
import {
createObjectShapePredicate,
type InferTypeFromShape,
type ObjectShapeRecord,
} from '../../src/predicate-factory/createObjectShapePredicate.js';

describe('createObjectShapePredicate', () => {
enum Role {
Admin = 'admin',
User = 'user',
}

type User = {
name: string;
role: Role;
value: number;
age?: number;
contact: {
email: string;
phone?: string;
};
tuple: [key: string, value: number];
};

const user: unknown = {
name: 'Test',
role: Role.User,
value: 50,
age: 100,
contact: {
email: '[email protected]',
phone: undefined,
},
tuple: ['PI', Math.PI],
} satisfies User;

type UserShape = ObjectShapeRecord<User>;

const userShape = {
name: /^Test$/,
role: Role.User,
value: createNumberRangePredicate(0, 100),
age: isNumber,
contact: {
email: isString,
phone: isOptionalString,
},
tuple: ['PI', Math.PI],
} satisfies UserShape;

type InferredUserType = InferTypeFromShape<UserShape>;
type InferredUserTypeConst = InferTypeFromShape<typeof userShape>;

it('Returns a type predicate function', () => {
const fn = createObjectShapePredicate(userShape);

expectTypeOf(fn).toBeFunction();
expectTypeOf(fn).parameter(0).toMatchTypeOf<unknown>();
});

describe('Type parameters', () => {
it('Infers type parameter values', () => {
const fn = createObjectShapePredicate(userShape);

if (fn(user)) {
expectTypeOf(user).toMatchTypeOf<InferredUserTypeConst>();
}
});

it('Can provide the Type to constrain the Shape', () => {
const fn = createObjectShapePredicate<User>(userShape);

if (fn(user)) {
expectTypeOf(user).toMatchTypeOf<InferredUserType>();
}
});

it('Can provide the Type and Shape parameters', () => {
const fn = createObjectShapePredicate<{ name: string }, { name: string | RegExp }>({
name: 'test',
});

if (fn(user)) {
expectTypeOf(user).toMatchTypeOf<{ name: string }>();
}
});
});
});
63 changes: 63 additions & 0 deletions test/types/arrays.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { describe, expectTypeOf, it } from 'vitest';

import type { First, Last, Head, Tail, GetLength, IfLength, IfEmpty, IfArray } from '../../src/types/arrays.js';

describe('First', () => {
it('returns the first element of an array', () => {
expectTypeOf<First<[1, 2, 3]>>().toEqualTypeOf<1>();
});
});

describe('Last', () => {
it('returns the last element of an array', () => {
expectTypeOf<Last<[1, 2, 3]>>().toEqualTypeOf<3>();
});
});

describe('Head', () => {
it('returns all but the last element of an array', () => {
expectTypeOf<Head<[1, 2, 3]>>().toEqualTypeOf<[1, 2]>();
});
});

describe('Tail', () => {
it('returns all but the first element of an array', () => {
expectTypeOf<Tail<[1, 2, 3]>>().toEqualTypeOf<[2, 3]>();
});
});

describe('GetLength', () => {
it('returns the length of an array', () => {
expectTypeOf<GetLength<[1, 2, 3]>>().toEqualTypeOf<3>();
});
});

describe('IfLength', () => {
it('returns the true type if the array length matches the specified length', () => {
expectTypeOf<IfLength<[1, 2, 3], 3, 'yes', 'no'>>().toEqualTypeOf<'yes'>();
});

it('returns the false type if the array length does not match the specified length', () => {
expectTypeOf<IfLength<[1, 2, 3], 2, 'yes', 'no'>>().toEqualTypeOf<'no'>();
});
});

describe('IfEmpty', () => {
it('returns the true type if the array is empty', () => {
expectTypeOf<IfEmpty<[], 'yes', 'no'>>().toEqualTypeOf<'yes'>();
});

it('returns the false type if the array is not empty', () => {
expectTypeOf<IfEmpty<[1, 2, 3], 'yes', 'no'>>().toEqualTypeOf<'no'>();
});
});

describe('IfArray', () => {
it('returns the true type if the type is an array', () => {
expectTypeOf<IfArray<[], 'yes', 'no'>>().toEqualTypeOf<'yes'>();
});

it('returns the false type if the type is not an array', () => {
expectTypeOf<IfArray<number, 'yes', 'no'>>().toEqualTypeOf<'no'>();
});
});
8 changes: 6 additions & 2 deletions vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
environment: 'jsdom',
include: ['./test/**/*.test.ts'],
benchmark: {
include: ['./bench/**/*.bench.ts'],
},
environment: 'jsdom',
include: ['./test/**/*.test.ts'],
typecheck: {
enabled: true,
include: ['./test/**/*.test-d.ts'],
},
coverage: {
all: false,
provider: 'v8',
Expand Down
Loading