simplytyped
Advanced tools
Comparing version 3.2.3 to 3.3.0
13
impl.js
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./impl/objects")); | ||
__exportStar(require("./impl/objects"), exports); |
@@ -14,2 +14,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.taggedObject = exports.Readonly = exports.objectKeys = exports.isKeyOf = void 0; | ||
/** | ||
@@ -16,0 +17,0 @@ * Type guard for any key, `k`. |
15
index.js
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./types")); | ||
__export(require("./impl")); | ||
__exportStar(require("./types"), exports); | ||
__exportStar(require("./impl"), exports); |
{ | ||
"name": "simplytyped", | ||
"version": "3.2.3", | ||
"version": "3.3.0", | ||
"description": "yet another Typescript type library for advanced types", | ||
@@ -36,6 +36,7 @@ "main": "index", | ||
"@commitlint/config-conventional": "^8.0.0", | ||
"@types/node": "~12.6.0", | ||
"ava": "~2.2.0", | ||
"@types/node": "~13.9.0", | ||
"@ava/babel": "^1.0.1", | ||
"ava": "~3.7.1", | ||
"commitlint": "^8.0.0", | ||
"husky": "^3.0.0", | ||
"husky": "^4.0.2", | ||
"ts-node": "^8.0.3", | ||
@@ -49,4 +50,3 @@ "tslint": "^5.13.0" | ||
"concurrency": 32, | ||
"babel": {}, | ||
"compileEnhancements": false | ||
"babel": {} | ||
}, | ||
@@ -53,0 +53,0 @@ "commitlint": { |
# SimplyTyped | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/andnp/SimplyTyped.svg)](https://greenkeeper.io/) | ||
[![Build Status](https://travis-ci.org/andnp/SimplyTyped.svg?branch=NumberPerformance)](https://travis-ci.org/andnp/SimplyTyped) | ||
@@ -20,3 +20,3 @@ | ||
[AllKeys](#allkeys) - [AllRequired](#allrequired) - [CombineObjects](#combineobjects) - [DeepPartial](#deeppartial) - [DeepReadonly](#deepreadonly) - [DiffKeys](#diffkeys) - [GetKey](#getkey) - [HasKey](#haskey) - [Intersect](#intersect) - [KeysByType](#keysbytype) - [Merge](#merge) - [ObjectKeys](#objectkeys) - [ObjectType](#objecttype) - [Omit](#omit) - [Optional](#optional) - [Overwrite](#overwrite) - [PlainObject](#plainobject) - [PureKeys](#purekeys) - [Required](#required) - [SharedKeys](#sharedkeys) - [StrictUnion](#strictunion) - [StringKeys](#stringkeys) - [TaggedObject](#taggedobject) - [UnionizeProperties](#unionizeproperties) - [UnionKeys](#unionkeys) | ||
[AllKeys](#allkeys) - [AllRequired](#allrequired) - [CombineObjects](#combineobjects) - [DeepPartial](#deeppartial) - [DeepReadonly](#deepreadonly) - [DiffKeys](#diffkeys) - [ElementwiseIntersect](#elementwiseintersect) - [GetKey](#getkey) - [HasKey](#haskey) - [Intersect](#intersect) - [KeysByType](#keysbytype) - [Merge](#merge) - [ObjectKeys](#objectkeys) - [ObjectType](#objecttype) - [Omit](#omit) - [Optional](#optional) - [Overwrite](#overwrite) - [PlainObject](#plainobject) - [PureKeys](#purekeys) - [Required](#required) - [SharedKeys](#sharedkeys) - [StrictUnion](#strictunion) - [StringKeys](#stringkeys) - [TaggedObject](#taggedobject) - [TryKey](#trykey) - [UnionizeProperties](#unionizeproperties) - [UnionKeys](#unionkeys) | ||
@@ -92,3 +92,3 @@ **[Utils](#utils)** | ||
type a = { x: number, y: 'hi' }; | ||
type b = { z: number, y: 'there' }; | ||
type b = { z: number }; | ||
@@ -98,3 +98,3 @@ type got = CombineObjects<a, b>; | ||
x: number, | ||
y: 'hi' & 'there', | ||
y: 'hi', | ||
z: number | ||
@@ -237,2 +237,47 @@ }; | ||
### ElementwiseIntersect | ||
Takes two objects and returns their element-wise intersection. | ||
*Note*: this removes any key-level information, such as optional or readonly keys. | ||
```ts | ||
test('Can combine two objects elementwise', t => { | ||
type a = { x: number, y: 'hi' }; | ||
type b = { z: number, y: 'there' }; | ||
type got = ElementwiseIntersect<a, b>; | ||
type expected = { | ||
x: number, | ||
y: 'hi' & 'there', | ||
z: number, | ||
}; | ||
assert<got, expected>(t); | ||
assert<expected, got>(t); | ||
}); | ||
test('Can combine two objects with private members elementwise', t => { | ||
class A { | ||
a: number = 1; | ||
private x: number = 2; | ||
y: 'hi' = 'hi'; | ||
private z: 'hey' = 'hey'; | ||
} | ||
class B { | ||
a: 22 = 22; | ||
private x: number = 2; | ||
y: 'there' = 'there'; | ||
private z: 'friend' = 'friend'; | ||
} | ||
type got = ElementwiseIntersect<A, B>; | ||
type expected = { | ||
a: 22, | ||
y: 'hi' & 'there', | ||
}; | ||
assert<got, expected>(t); | ||
assert<expected, got>(t); | ||
}); | ||
``` | ||
### GetKey | ||
@@ -478,2 +523,6 @@ Gets the value of specified property on any object without compile time error (`Property 'b' does not exist on type '{ a: string; }'.`) and the like. | ||
### TryKey | ||
Like `GetKey`, but returns `unknown` if the key is not present on the object. | ||
### UnionizeProperties | ||
@@ -480,0 +529,0 @@ Get a union of the properties of an object. |
20
types.js
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./types/utils")); | ||
__exportStar(require("./types/conditionals"), exports); | ||
__exportStar(require("./types/functions"), exports); | ||
__exportStar(require("./types/numbers"), exports); | ||
__exportStar(require("./types/objects"), exports); | ||
__exportStar(require("./types/predicates"), exports); | ||
__exportStar(require("./types/strings"), exports); | ||
__exportStar(require("./types/tuples"), exports); | ||
__exportStar(require("./types/utils"), exports); |
@@ -34,2 +34,19 @@ import { False, True } from './conditionals'; | ||
/** | ||
* Like `GetKey`, but returns `unknown` if the key is not present on the object. | ||
* @param T Object to get values from | ||
* @param K Key to query object for value | ||
* @returns `T[K]` if the key exists, `unknown` otherwise | ||
*/ | ||
export declare type TryKey<T, K extends keyof any> = K extends keyof T ? T[K] : unknown; | ||
/** | ||
* Takes two objects and returns their element-wise intersection. | ||
* *Note*: this removes any key-level information, such as optional or readonly keys. | ||
* @param T First object to be intersected | ||
* @param U Second object to be intersected | ||
* @returns element-wise `T` & `U` cleaned up to look like flat object to VSCode | ||
*/ | ||
export declare type ElementwiseIntersect<T extends object, U extends object> = { | ||
[k in (keyof T | keyof U)]: TryKey<T, k> & TryKey<U, k>; | ||
}; | ||
/** | ||
* Objects can be indexed by multiple types: `string`, `number`, `symbol`. | ||
@@ -36,0 +53,0 @@ * For safe compatibility with typescript version, this type will always |
55734
579
1102
8