├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── package.json ├── src ├── index.test.ts └── index.ts ├── tsconfig-build.json └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | dist/ 2 | node_modules/ 3 | -------------------------------------------------------------------------------- /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.0.0 (2018-12-21) 8 | 9 | ### Features 10 | 11 | - initial commit d1827dd 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 LeDDGroup 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # typescript-conditional-types 2 | 3 | [![npm version](https://img.shields.io/npm/v/typescript-conditional-types.svg)](https://www.npmjs.com/package/typescript-conditional-types) 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) 6 | 7 | Helpers for typescript generic types 8 | 9 | **Table of Contents** 10 | 11 | - [Motivation](#motivation) 12 | - [Install](#install) 13 | - [Type Helper List](#type-helper-list) 14 | - [Usage Example](#usage-example) 15 | 16 | ## Motivation 17 | 18 | Creating complex types with conditional types ( `T extends U ? X : Y` ) could be a little verbose. This package aims to simplify code and make it more readable. 19 | 20 | 21 | ## Install 22 | 23 | ```bash 24 | $ npm install typescript-conditional-types 25 | ``` 26 | 27 | You'll probably want to save it in the _devDependencies_ 28 | 29 | ## Type Helper List 30 | 31 | - _If_: If _Condition_ is `true` resulting type is _Then_ else _Else_ 32 | - _And_: `true` if _A_ and _B_ are both `true` else `false` 33 | - _Or_: `true` if _A_ or _B_ are `true` else `false` 34 | - _Not_: Negate _A_ 35 | - _Extends_: `true` if _A_ extends _B_ like in `A extends B ? true : false` 36 | - _Extends, Then, Else>` 37 | 38 | ## Usage Example 39 | 40 | ```ts 41 | import { If } from "typescript-conditional-types"; 42 | 43 | type BooleanToString = If 44 | 45 | BooleanToString // "true" 46 | BooleanToString // "false" 47 | ``` 48 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "typescript-conditional-types", 3 | "description": "Helpers for typescript generic types", 4 | "version": "1.0.0", 5 | "main": "dist/index.js", 6 | "types": "dist/index.d.ts", 7 | "files": [ 8 | "dist" 9 | ], 10 | "scripts": { 11 | "prebuild": "rm -rf dist", 12 | "build": "tsc -p tsconfig-build.json", 13 | "prepare": "npm run build", 14 | "release": "standard-version", 15 | "test": "tsc --noEmit" 16 | }, 17 | "husky": { 18 | "hooks": { 19 | "pre-commit": "npm test && pretty-quick --staged", 20 | "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" 21 | } 22 | }, 23 | "keywords": [ 24 | "and", 25 | "conditional", 26 | "extends", 27 | "generic", 28 | "if", 29 | "not", 30 | "or", 31 | "types", 32 | "typescript", 33 | "utility", 34 | "utitlities" 35 | ], 36 | "license": "MIT", 37 | "devDependencies": { 38 | "@commitlint/cli": "^7.2.1", 39 | "@commitlint/config-conventional": "^7.1.2", 40 | "husky": "^1.1.2", 41 | "prettier": "^1.13.7", 42 | "pretty-quick": "^1.8.0", 43 | "standard-version": "^4.4.0", 44 | "typescript": "^3.1.6" 45 | }, 46 | "dependencies": {} 47 | } 48 | -------------------------------------------------------------------------------- /src/index.test.ts: -------------------------------------------------------------------------------- 1 | import { Extends, If, And, Not, Or } from "./index"; 2 | 3 | function assert() {} 4 | 5 | // Extends 6 | assert>(); 7 | assert>>(); 8 | 9 | // And 10 | assert>(); 11 | assert>>(); 12 | assert>>(); 13 | assert>>(); 14 | 15 | // Or 16 | assert>(); 17 | assert>(); 18 | assert>(); 19 | assert>>(); 20 | 21 | // If 22 | assert>(); 23 | assert>>(); 24 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | export type Extends = A extends B 2 | ? Then 3 | : Else; 4 | 5 | export type If = Extends; 6 | 7 | export type And = Extends< 8 | A, 9 | true, 10 | B, 11 | false 12 | >; 13 | 14 | export type Or = Extends< 15 | A, 16 | true, 17 | true, 18 | B 19 | >; 20 | 21 | export type Not = Extends; 22 | -------------------------------------------------------------------------------- /tsconfig-build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "./dist", 5 | "declaration": true, 6 | "rootDir": "./src" 7 | }, 8 | "exclude": ["**/*.test.ts"] 9 | } 10 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "module": "commonjs", 5 | "strict": true, 6 | "esModuleInterop": true 7 | } 8 | } 9 | --------------------------------------------------------------------------------