├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── README.md ├── package.json ├── src ├── index.test.ts └── index.ts └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | dist/ 2 | node_modules/ 3 | yarn.lock -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - stable 4 | deploy: 5 | provider: npm 6 | email: danielpza@protonmail.com 7 | skip_cleanup: true 8 | api_key: 9 | secure: S0cD6UbaJYG6JIZnT+m4+DUhG0kPBi0bWnU4ORliJJJelY5eDbkkufJSfZ9W4XYfwNdTF92NBw6lOxXMat5h2m1140qLYxyiup4ZIN7PkhzRvjJrSBL2OeV3yr9+hpEZDln7uuHPGWCVXVtBXQKU9uhkImxkubkL5+9VZUuSWTzujo3j1+E7c17SAR3zrooEO37oZVkRJrUqAFGasqqUCVs/Omwjz3iNP6gePP2PqCu9aj6fqXE2OUPqv5zUJsUYkFvQ0luSBFUJU2TuT1iE5QbgOETuy2YYDytySbCMf9XVKlFbSvCxBoq1Ou3fj2RM3rKJR2bK1SYUakF6HCjeDVYnF+KtWCBrfY3PEqpgMQzG8pLbubfENUlyO1wVKztyPRuQJhAG/OxQp92KHNmsIW761YgN7J+lh5jUdP4LhUaN6uSjhi1k1yvJM6NpIL8w+R4aE0v9t+UQUspyu5cCLGmuoOTznBnAdzLKxOSWxaWyx2bXgonb28uZ9G+jwleMs3sU6Pl2U1pNOWn7hTExzi7wgquM5y5kNFHDkh1tc32KNcuGJfeTrGkB71ir5TbcFvRYnJ7i460CHtpz/XsU4UeIXo+80h8OtQepv0T2m5lyx+zfs/Yv52DN6XfHmsMB8i9/ucrxBNhAwfpm6eC2gBUw4W0/S47O5mNPLFjn7As= 10 | on: 11 | tags: true 12 | repo: danielpa9708/typescript-test-utils 13 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | 6 | 7 | ## [1.2.1](https://github.com/danielpa9708/typescript-test-utils/compare/v1.2.0...v1.2.1) (2018-11-22) 8 | 9 | ### Bug Fixes 10 | 11 | - fix `Equals` ([e859cfe](https://github.com/danielpa9708/typescript-test-utils/commit/e859cfe)) 12 | 13 | 14 | 15 | # [1.2.0](https://github.com/danielpa9708/typescript-test-utils/compare/v1.1.0...v1.2.0) (2018-11-08) 16 | 17 | ### Features 18 | 19 | - add `assertTrue` and `assertFalse` ([c3ee663](https://github.com/danielpa9708/typescript-test-utils/commit/c3ee663)) 20 | 21 | 22 | 23 | # [1.1.0](https://github.com/danielpa9708/typescript-test-utils/compare/v1.0.0...v1.1.0) (2018-11-06) 24 | 25 | ### Features 26 | 27 | - add `Not` type helper ([cc60aaf](https://github.com/danielpa9708/typescript-test-utils/commit/cc60aaf)) 28 | 29 | 30 | 31 | # 1.0.0 (2018-11-04) 32 | 33 | ### Features 34 | 35 | - initial commit ([1c4b6f4](https://github.com/danielpa9708/typescript-test-utils/commit/1c4b6f4)) 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # typescript-test-utils 2 | 3 | [![npm version](https://img.shields.io/npm/v/typescript-test-utils.svg "test")](https://www.npmjs.com/package/typescript-test-utils) 4 | [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org) 5 | [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier) [![Greenkeeper badge](https://badges.greenkeeper.io/LeDDGroup/typescript-test-utils.svg)](https://greenkeeper.io/) 6 | 7 | Helper types for testing your package exported types 8 | 9 | - Only depends on typescript installed. 10 | - Easily extendable 11 | 12 | ## Note 13 | 14 | I have notice some issues with typescript if your types are too complex, so I recommend using `assertTrue` and `assertFalse` instead of `assert` 15 | 16 | ## Usage 17 | 18 | You test them with the assert method ( wich is just a placeholder, it doesn't run anything ) and the type helpers 19 | 20 | ```ts 21 | import { 22 | assert, 23 | assertTrue, 24 | assertFalse, 25 | HasProperties 26 | } from "typescript-test-utils"; 27 | 28 | assertTrue(); // ok 29 | assertTrue(); // nop 30 | assertFalse(); // nop 31 | assertFalse(); // ok 32 | 33 | assert(true); // ok 34 | assert(true); // nop 35 | 36 | type MyType = { a: string }; 37 | assertTrue>(); // ok 38 | assertFalse>(); // nop 39 | assertTrue>(); // nop 40 | assertFalse>(); // ok 41 | ``` 42 | 43 | And just run tsc on your test files to check for type errors 44 | 45 | ```json 46 | { 47 | "scripts": { 48 | "test": "tsc --noEmit src/*.test.ts" 49 | } 50 | } 51 | ``` 52 | 53 | ## Assertions 54 | 55 | There are currently some implemented, if you have any idea for a new one send a PR or open an issue 56 | 57 | ```ts 58 | import { assert, HasProperties, Extends, Equals, Not } from "typescript-test-utils"; 59 | 60 | HasProperties<{ a: string, b: number }, "a" | "b"> // true 61 | HasProperties<{ a: string, b: number }, "a" | "c"> // false 62 | 63 | Extends<{ a: string, b: string }, { a: string }> // true 64 | Extends<{ a: string, b: string }, { c: string }> // false 65 | 66 | Equals<{ a: string }, { a: string }> // true 67 | Equals<{ a: string, b: string }, { a: string }> // false 68 | 69 | Not // false 70 | Not // true 71 | 72 | Not> // true 73 | ``` 74 | 75 | ## Adding your own assertions 76 | 77 | You only need to make a type that returns true or false, for example: 78 | 79 | ```ts 80 | type Not = T extends true ? false : true; 81 | ``` 82 | 83 | or 84 | 85 | ```ts 86 | type Extends = T extends K ? true : false; 87 | ``` 88 | 89 | These are the definitions of the `Not` and `Extends` helpers 90 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "typescript-test-utils", 3 | "version": "1.2.1", 4 | "keywords": [ 5 | "test", 6 | "types", 7 | "typescript" 8 | ], 9 | "license": "MIT", 10 | "author": "Daniel Perez Alvarez ", 11 | "files": [ 12 | "dist" 13 | ], 14 | "main": "dist/index.js", 15 | "types": "dist/index.d.ts", 16 | "scripts": { 17 | "prebuild": "rm -rf dist", 18 | "build": "tsc", 19 | "prepare": "npm run build", 20 | "release": "standard-version", 21 | "test": "tsc --noEmit src/*.test.ts" 22 | }, 23 | "husky": { 24 | "hooks": { 25 | "pre-commit": "npm test && pretty-quick --staged", 26 | "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" 27 | } 28 | }, 29 | "devDependencies": { 30 | "@commitlint/cli": "^7.2.1", 31 | "@commitlint/config-conventional": "^7.1.2", 32 | "husky": "^1.1.3", 33 | "prettier": "^1.14.3", 34 | "pretty-quick": "^1.8.0", 35 | "standard-version": "^4.4.0", 36 | "typescript": "^3.1.6" 37 | }, 38 | "description": "Helper types for testing your package exported types", 39 | "repository": { 40 | "type": "git", 41 | "url": "git+https://github.com/danielpa9708/typescript-test-utils.git" 42 | }, 43 | "bugs": { 44 | "url": "https://github.com/danielpa9708/typescript-test-utils/issues" 45 | }, 46 | "homepage": "https://github.com/danielpa9708/typescript-test-utils#readme" 47 | } 48 | -------------------------------------------------------------------------------- /src/index.test.ts: -------------------------------------------------------------------------------- 1 | import { 2 | assert, 3 | HasProperties, 4 | Extends, 5 | Equals, 6 | Not, 7 | assertTrue, 8 | assertFalse 9 | } from "./index"; 10 | 11 | type DataType = { a: string; b: string }; 12 | 13 | // HasProperties 14 | assert>(true); 15 | assert>(true); 16 | assert>(false); 17 | assert>(false); 18 | 19 | // Extends 20 | assert>(false); 21 | assert>(true); 22 | 23 | //Equals 24 | assert>(true); 25 | assert>>(false); 26 | assertFalse>(); 27 | assertFalse>(); 28 | assertTrue>(); 29 | 30 | // Extra 31 | assertTrue>(); 32 | assertFalse>(); 33 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | type ObjectMap = { [id in K]: V }; 2 | 3 | type If = Condition extends true 4 | ? Then 5 | : Else; 6 | type And = If< 7 | A, 8 | If, 9 | false 10 | >; 11 | export type Extends = A extends B ? true : false; 12 | export type HasProperties = Extends>; 13 | export type Not = If, false, true>; 14 | export type Equals = And, Extends<[B], [A]>>; 15 | 16 | export function assert(expected: T) {} 17 | export function assertTrue() {} 18 | export function assertFalse() {} 19 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | /* Basic Options */ 4 | "target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */, 5 | "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */, 6 | // "lib": [], /* Specify library files to be included in the compilation. */ 7 | // "allowJs": true, /* Allow javascript files to be compiled. */ 8 | // "checkJs": true, /* Report errors in .js files. */ 9 | // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ 10 | "declaration": true /* Generates corresponding '.d.ts' file. */, 11 | // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ 12 | // "sourceMap": true, /* Generates corresponding '.map' file. */ 13 | // "outFile": "./", /* Concatenate and emit output to single file. */ 14 | "outDir": "./dist" /* Redirect output structure to the directory. */, 15 | "rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */, 16 | // "composite": true, /* Enable project compilation */ 17 | // "removeComments": true, /* Do not emit comments to output. */ 18 | // "noEmit": true, /* Do not emit outputs. */ 19 | // "importHelpers": true, /* Import emit helpers from 'tslib'. */ 20 | // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ 21 | // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ 22 | 23 | /* Strict Type-Checking Options */ 24 | "strict": true /* Enable all strict type-checking options. */, 25 | // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ 26 | // "strictNullChecks": true, /* Enable strict null checks. */ 27 | // "strictFunctionTypes": true, /* Enable strict checking of function types. */ 28 | // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ 29 | // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ 30 | // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ 31 | 32 | /* Additional Checks */ 33 | // "noUnusedLocals": true, /* Report errors on unused locals. */ 34 | // "noUnusedParameters": true, /* Report errors on unused parameters. */ 35 | // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ 36 | // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ 37 | 38 | /* Module Resolution Options */ 39 | // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ 40 | // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ 41 | // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ 42 | // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ 43 | // "typeRoots": [], /* List of folders to include type definitions from. */ 44 | // "types": [], /* Type declaration files to be included in compilation. */ 45 | // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ 46 | "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ 47 | // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ 48 | 49 | /* Source Map Options */ 50 | // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ 51 | // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ 52 | // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ 53 | // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ 54 | 55 | /* Experimental Options */ 56 | // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ 57 | // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ 58 | }, 59 | "exclude": ["**/*.test.ts"] 60 | } 61 | --------------------------------------------------------------------------------