inferred-types 0.18.4 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -899,6 +899,11 @@ declare type SnakeCase<S extends string> = string extends S ? string : DashUpper
899
899
  */
900
900
  declare type Get<T, K> = K extends `${infer FK}.${infer L}` ? FK extends keyof T ? Get<T[FK], L> : never : K extends keyof T ? T[K] : never;
901
901
 
902
+ /**
903
+ * Accepts an object with at least one property defined on it
904
+ */
905
+ declare type NotEmptyObject<T extends {}> = ExpandRecursively<Length<UnionToTuple<Keys<T>>> extends 0 ? false : T>;
906
+
902
907
  /**
903
908
  * **ToFluent**
904
909
  *
@@ -1037,7 +1042,7 @@ declare type FirstKeyValue<T extends object> = FirstKey<T> extends keyof T ? T[F
1037
1042
  declare type FirstOfEach<T extends readonly any[][]> = T[number][0] extends T[number][number] ? T[number][0] : never;
1038
1043
 
1039
1044
  /**
1040
- * Typescript utility which recieves `T` as shape which resembles `DictArray<D>`
1045
+ * Typescript utility which receives `T` as shape which resembles `DictArray<D>`
1041
1046
  * and if the type `D` can be inferred it is returned.
1042
1047
  * ```ts
1043
1048
  * // { foo: 1; bar: "hi" }
@@ -1082,6 +1087,19 @@ declare type LastInUnion<U> = UnionToIntersection<U extends unknown ? (x: U) =>
1082
1087
  */
1083
1088
  declare type UnionToTuple<U, Last = LastInUnion<U>> = [U] extends [never] ? [] : [...UnionToTuple<Exclude<U, Last>>, Last];
1084
1089
 
1090
+ /**
1091
+ * Given a dictionary of key/values, where the value is a function, this
1092
+ * type utility will maintain the keys but change the values to whatever
1093
+ * the `ReturnType` of the function was.
1094
+ * ```ts
1095
+ * // { foo: string }
1096
+ * type Test = UnwrapValue<{ foo: (name: string) => name }>
1097
+ * ```
1098
+ */
1099
+ declare type UnwrapValue<T extends Record<string, (...args: any[]) => any>> = {
1100
+ [K in keyof T]: T[K] extends Function ? ReturnType<T[K]> : never;
1101
+ }[keyof T];
1102
+
1085
1103
  declare type ValueFunction<T extends any> = <R extends any>(v: T) => R;
1086
1104
  /**
1087
1105
  * **SameKeys**
@@ -1096,6 +1114,14 @@ declare type WrapValue<T extends {}, F extends ValueFunction<T>> = {
1096
1114
  };
1097
1115
 
1098
1116
  declare function createFnWithProps<F extends Function, P extends {}>(fn: F, props: P): F & P;
1117
+ /**
1118
+ * Adds a dictionary of key/value pairs to a function.
1119
+ */
1120
+ declare function fnWithProps<A extends any[], R extends any, P extends {}>(fn: ((...args: A) => R), props: P): ((...args: A) => R) & P;
1121
+ /**
1122
+ * Adds read-only (and narrowly typed) key/value pairs to a function
1123
+ */
1124
+ declare function readonlyFnWithProps<A extends any[], R extends any, N extends Narrowable, P extends Record<keyof P, N>>(fn: ((...args: A) => R), props: P): ((...args: A) => R) & Readonly<P>;
1099
1125
 
1100
1126
  /**
1101
1127
  * Provides the _keys_ of an object with the `keyof T` made explicit.
@@ -9770,4 +9796,4 @@ declare function KeyStorage(): {
9770
9796
  done: () => never[];
9771
9797
  };
9772
9798
 
9773
- export { AllCaps, Alpha, AlphaNumeric, Api, ApiFunction, ApiValue, AppendToDictionary, AppendToObject, ArrConcat, Array$1 as Array, ArrayConverter, AssertExtends, Bracket, Break, CamelCase, CapitalizeWords, ClosingBracket, Condition, Configurator, Constructor, DashToSnake, DashUppercase, Dasherize, DictArray, DictArrayFilterCallback, DictArrayKv, DictFromKv, DictKvTuple, DynamicRule, DynamicRuleSet, Email, EnumValues, ExpandRecursively, ExpectExtends, ExplicitFunction, ExtendsClause, ExtendsNarrowlyClause, Filter, First, FirstKey, FirstKeyValue, FirstOfEach, FluentConfigurator, FluentFunction, FromDictArray, FunctionType, GeneralDictionary, Get, HasUppercase, IConfigurator, IFluentConfigurator, If, IfExtends, IfExtendsThen, Include, Includes, IsArray, IsBoolean, IsCapitalized, IsFalse, IsFunction, IsLiteral, IsObject, IsString, IsTrue, KebabCase, KeyStorage, KeyStorageApi, KeyValue, KeyedRecord, Keys, KeysWithValue, KvFrom, KvTuple, LastInUnion, LeftWhitespace, Length, LowerAllCaps, LowerAlpha, MaybeFalse, MaybeTrue, Model, Mutable, MutationFunction, MutationIdentity, Narrowable, NonAlpha, NonNumericKeys, NonStringKeys, Not, Numeric, NumericKeys, NumericString, ObjectType, Opaque, OpenningBracket, OptionalKeys, OptionalProps, Parenthesis, PascalCase, Pluralize, PrivateKey, PrivateKeys, PublicKeys, Punctuation, PureFluentApi, Replace, RequiredKeys, RequiredProps, Retain, RightWhitespace, RuleDefinition, RuntimeProp, RuntimeType, SameKeys, SecondOfEach, SnakeCase, SpecialCharacters, StringDelimiter, StringKeys, StringLength, ToFluent, Transformer, Trim, TrimLeft, TrimRight, TupleToUnion, Type, TypeApi, TypeCondition, TypeDefinition, TypeGuard, TypeOptions, UnionToIntersection, UnionToTuple, UniqueDictionary, UniqueForProp, UpperAlpha, ValueTuple, ValueTypeFunc, ValueTypes, Where, WhereNot, Whitespace, WithNumericKeys, WithStringKeys, WithValue, WrapValue, ZipCode, and, api, arrayToKeyLookup, arrayToObject, condition, createFnWithProps, createMutationFunction, defineType, dictToKv, dictionaryTransform, entries, equals, filterDictArray, greater, groupBy, idLiteral, idTypeGuard, identity, ifTypeOf, isArray, isBoolean, isFalse, isFunction, isLiteral, isNull, isNumber, isObject, isString, isSymbol, isTrue, isType, isUndefined, keys, kindLiteral, kv, kvToDict, less, literal, mapValues, nameLiteral, or, randomString, ruleSet, strArrayToDict, type, typeApi, uuid, valueTypes, valuesOfProp, withValue };
9799
+ export { AllCaps, Alpha, AlphaNumeric, Api, ApiFunction, ApiValue, AppendToDictionary, AppendToObject, ArrConcat, Array$1 as Array, ArrayConverter, AssertExtends, Bracket, Break, CamelCase, CapitalizeWords, ClosingBracket, Condition, Configurator, Constructor, DashToSnake, DashUppercase, Dasherize, DictArray, DictArrayFilterCallback, DictArrayKv, DictFromKv, DictKvTuple, DynamicRule, DynamicRuleSet, Email, EnumValues, ExpandRecursively, ExpectExtends, ExplicitFunction, ExtendsClause, ExtendsNarrowlyClause, Filter, First, FirstKey, FirstKeyValue, FirstOfEach, FluentConfigurator, FluentFunction, FromDictArray, FunctionType, GeneralDictionary, Get, HasUppercase, IConfigurator, IFluentConfigurator, If, IfExtends, IfExtendsThen, Include, Includes, IsArray, IsBoolean, IsCapitalized, IsFalse, IsFunction, IsLiteral, IsObject, IsString, IsTrue, KebabCase, KeyStorage, KeyStorageApi, KeyValue, KeyedRecord, Keys, KeysWithValue, KvFrom, KvTuple, LastInUnion, LeftWhitespace, Length, LowerAllCaps, LowerAlpha, MaybeFalse, MaybeTrue, Model, Mutable, MutationFunction, MutationIdentity, Narrowable, NonAlpha, NonNumericKeys, NonStringKeys, Not, NotEmptyObject, Numeric, NumericKeys, NumericString, ObjectType, Opaque, OpenningBracket, OptionalKeys, OptionalProps, Parenthesis, PascalCase, Pluralize, PrivateKey, PrivateKeys, PublicKeys, Punctuation, PureFluentApi, Replace, RequiredKeys, RequiredProps, Retain, RightWhitespace, RuleDefinition, RuntimeProp, RuntimeType, SameKeys, SecondOfEach, SnakeCase, SpecialCharacters, StringDelimiter, StringKeys, StringLength, ToFluent, Transformer, Trim, TrimLeft, TrimRight, TupleToUnion, Type, TypeApi, TypeCondition, TypeDefinition, TypeGuard, TypeOptions, UnionToIntersection, UnionToTuple, UniqueDictionary, UniqueForProp, UnwrapValue, UpperAlpha, ValueTuple, ValueTypeFunc, ValueTypes, Where, WhereNot, Whitespace, WithNumericKeys, WithStringKeys, WithValue, WrapValue, ZipCode, and, api, arrayToKeyLookup, arrayToObject, condition, createFnWithProps, createMutationFunction, defineType, dictToKv, dictionaryTransform, entries, equals, filterDictArray, fnWithProps, greater, groupBy, idLiteral, idTypeGuard, identity, ifTypeOf, isArray, isBoolean, isFalse, isFunction, isLiteral, isNull, isNumber, isObject, isString, isSymbol, isTrue, isType, isUndefined, keys, kindLiteral, kv, kvToDict, less, literal, mapValues, nameLiteral, or, randomString, readonlyFnWithProps, ruleSet, strArrayToDict, type, typeApi, uuid, valueTypes, valuesOfProp, withValue };
package/dist/index.js CHANGED
@@ -60,6 +60,7 @@ __export(src_exports, {
60
60
  entries: () => entries,
61
61
  equals: () => equals,
62
62
  filterDictArray: () => filterDictArray,
63
+ fnWithProps: () => fnWithProps,
63
64
  greater: () => greater,
64
65
  groupBy: () => groupBy,
65
66
  idLiteral: () => idLiteral,
@@ -89,6 +90,7 @@ __export(src_exports, {
89
90
  nameLiteral: () => nameLiteral,
90
91
  or: () => or,
91
92
  randomString: () => randomString,
93
+ readonlyFnWithProps: () => readonlyFnWithProps,
92
94
  ruleSet: () => ruleSet,
93
95
  strArrayToDict: () => strArrayToDict,
94
96
  type: () => type,
@@ -158,6 +160,20 @@ function createFnWithProps(fn, props) {
158
160
  return combined;
159
161
  })();
160
162
  }
163
+ function fnWithProps(fn, props) {
164
+ let combined = fn;
165
+ for (const prop of keys(props)) {
166
+ combined[prop] = props[prop];
167
+ }
168
+ return combined;
169
+ }
170
+ function readonlyFnWithProps(fn, props) {
171
+ let combined = fn;
172
+ for (const prop of keys(props)) {
173
+ combined[prop] = props[prop];
174
+ }
175
+ return combined;
176
+ }
161
177
 
162
178
  // src/utility/ruleset.ts
163
179
  function ruleSet(defn) {
@@ -641,6 +657,7 @@ module.exports = __toCommonJS(src_exports);
641
657
  entries,
642
658
  equals,
643
659
  filterDictArray,
660
+ fnWithProps,
644
661
  greater,
645
662
  groupBy,
646
663
  idLiteral,
@@ -670,6 +687,7 @@ module.exports = __toCommonJS(src_exports);
670
687
  nameLiteral,
671
688
  or,
672
689
  randomString,
690
+ readonlyFnWithProps,
673
691
  ruleSet,
674
692
  strArrayToDict,
675
693
  type,
package/dist/index.mjs CHANGED
@@ -77,6 +77,20 @@ function createFnWithProps(fn, props) {
77
77
  return combined;
78
78
  })();
79
79
  }
80
+ function fnWithProps(fn, props) {
81
+ let combined = fn;
82
+ for (const prop of keys(props)) {
83
+ combined[prop] = props[prop];
84
+ }
85
+ return combined;
86
+ }
87
+ function readonlyFnWithProps(fn, props) {
88
+ let combined = fn;
89
+ for (const prop of keys(props)) {
90
+ combined[prop] = props[prop];
91
+ }
92
+ return combined;
93
+ }
80
94
 
81
95
  // src/utility/ruleset.ts
82
96
  function ruleSet(defn) {
@@ -558,6 +572,7 @@ export {
558
572
  entries,
559
573
  equals,
560
574
  filterDictArray,
575
+ fnWithProps,
561
576
  greater,
562
577
  groupBy,
563
578
  idLiteral,
@@ -587,6 +602,7 @@ export {
587
602
  nameLiteral,
588
603
  or,
589
604
  randomString,
605
+ readonlyFnWithProps,
590
606
  ruleSet,
591
607
  strArrayToDict,
592
608
  type,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inferred-types",
3
- "version": "0.18.4",
3
+ "version": "0.20.0",
4
4
  "description": "Functions which provide useful type inference on TS projects",
5
5
  "license": "MIT",
6
6
  "author": "Ken Snyder<ken@ken.net>",
@@ -14,6 +14,9 @@
14
14
  "build": "run-s clean autoindex lint build:bundle",
15
15
  "build:force": "run-s clean autoindex build:bundle",
16
16
  "build:bundle": "npx tsup src/index.ts --dts --format='esm,cjs'",
17
+ "watch": "run-p watch:*",
18
+ "watch:autoindex": "npx dd autoindex --watch",
19
+ "watch:bundle": "npx tsup src/index.ts --dts --format='esm,cjs' --watch",
17
20
  "clean": "rimraf dist/**/*",
18
21
  "lint": "eslint src/**/*.ts --fix && tsc --noEmit ",
19
22
  "lint:full": "eslint src/**/*.ts && eslint test/**/*.ts && tsc --noEmit",
@@ -48,4 +51,4 @@
48
51
  "dependencies": {
49
52
  "common-types": "^1.31.0"
50
53
  }
51
- }
54
+ }
@@ -6,4 +6,4 @@
6
6
  */
7
7
  export type FunctionType<T extends {} = {}> = {} extends T
8
8
  ? Function | (Function & { [key: string]: any })
9
- : Function | (Function & T);
9
+ : Function | (Function & T);
@@ -0,0 +1,9 @@
1
+ import { ExpandRecursively } from "../ExpandRecursively";
2
+ import { Keys, Length, UnionToTuple } from "../index";
3
+
4
+ /**
5
+ * Accepts an object with at least one property defined on it
6
+ */
7
+ export type NotEmptyObject<T extends {}> = ExpandRecursively<
8
+ Length<UnionToTuple<Keys<T>>> extends 0 ? false : T
9
+ >;
@@ -1,10 +1,11 @@
1
1
  // #autoindex
2
2
  // #region autoindexed files
3
- // index last changed at: 1st Jan, 2022, 03:27 PM ( GMT-8 )
4
- // hash-code: 47804a72
3
+ // index last changed at: 6th Jun, 2022, 03:36 AM ( GMT-7 )
4
+ // hash-code: 1f3a5076
5
5
 
6
6
  // file exports
7
7
  export * from "./Get";
8
+ export * from "./NotEmptyObject";
8
9
 
9
10
  // #endregion
10
11
 
@@ -2,7 +2,7 @@ import { ExpandRecursively, UnionToIntersection } from "~/types";
2
2
 
3
3
 
4
4
  /**
5
- * Typescript utility which recieves `T` as shape which resembles `DictArray<D>`
5
+ * Typescript utility which receives `T` as shape which resembles `DictArray<D>`
6
6
  * and if the type `D` can be inferred it is returned.
7
7
  * ```ts
8
8
  * // { foo: 1; bar: "hi" }
@@ -0,0 +1,13 @@
1
+
2
+ /**
3
+ * Given a dictionary of key/values, where the value is a function, this
4
+ * type utility will maintain the keys but change the values to whatever
5
+ * the `ReturnType` of the function was.
6
+ * ```ts
7
+ * // { foo: string }
8
+ * type Test = UnwrapValue<{ foo: (name: string) => name }>
9
+ * ```
10
+ */
11
+ export type UnwrapValue<T extends Record<string, (...args: any[]) => any>> = {
12
+ [K in keyof T]: T[K] extends Function ? ReturnType<T[K]> : never;
13
+ }[keyof T];
@@ -1,14 +1,15 @@
1
1
  // #autoindex
2
2
 
3
3
  // #region autoindexed files
4
- // index last changed at: 1st Jan, 2022, 03:27 PM ( GMT-8 )
5
- // hash-code: 3c8daf19
4
+ // index last changed at: 6th Jun, 2022, 03:36 AM ( GMT-7 )
5
+ // hash-code: ac142b94
6
6
 
7
7
  // file exports
8
8
  export * from "./SameKeys";
9
9
  export * from "./TupleToUnion";
10
10
  export * from "./UnionToIntersection";
11
11
  export * from "./UnionToTuple";
12
+ export * from "./UnwrapValue";
12
13
  export * from "./WrapValue";
13
14
 
14
15
  // #endregion
@@ -1,3 +1,4 @@
1
+ import { Narrowable } from "../types/Narrowable";
1
2
  import { keys } from "./keys";
2
3
 
3
4
  export function createFnWithProps<F extends Function, P extends {}>(fn: F, props: P) {
@@ -10,3 +11,27 @@ export function createFnWithProps<F extends Function, P extends {}>(fn: F, props
10
11
  return combined;
11
12
  })() as unknown as F & P;
12
13
  }
14
+
15
+ /**
16
+ * Adds a dictionary of key/value pairs to a function.
17
+ */
18
+ export function fnWithProps<A extends any[], R extends any, P extends {}>(fn: ((...args: A) => R), props: P) {
19
+ // eslint-disable-next-line prefer-const
20
+ let combined: any = fn;
21
+ for (const prop of keys(props)) {
22
+ combined[prop] = props[prop];
23
+ }
24
+ return combined as ((...args: A) => R) & P;
25
+ }
26
+
27
+ /**
28
+ * Adds read-only (and narrowly typed) key/value pairs to a function
29
+ */
30
+ export function readonlyFnWithProps<A extends any[], R extends any, N extends Narrowable, P extends Record<keyof P, N>>(fn: ((...args: A) => R), props: P) {
31
+ // eslint-disable-next-line prefer-const
32
+ let combined: any = fn;
33
+ for (const prop of keys(props)) {
34
+ combined[prop] = props[prop];
35
+ }
36
+ return combined as ((...args: A) => R) & Readonly<P>;
37
+ }