@battis/typescript-tricks 0.3.0 → 0.4.1

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.
@@ -1,9 +1,21 @@
1
- export type Constructor<T> = new (...args: any[]) => T;
1
+ export type Constructor<T = object> = new (...args: any[]) => T;
2
2
  export declare const isConstructor: (value: any) => value is Constructor<any>;
3
3
  /**
4
- * TODO is this even useful?
5
- * Not sure why I included it, it seems more likely that it's related to
6
- * wanting [this](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards)?
4
+ * ```ts
5
+ * class A {}
6
+ * class B {}
7
+ * class C extends A {}
8
+ *
9
+ * const a = new A();
10
+ * const b = new B();
11
+ * const c = new C();
12
+ *
13
+ * const list = [a, b, c];
14
+ * const filteredList = instanceOf(list, A);
15
+ * // filteredList = [a, c]
16
+ * ```
7
17
  * @ see https://stackoverflow.com/a/65152869/294171
8
18
  */
9
- export declare function instanceOf<TElements, TFilter extends TElements>(array: TElements[], filterType: Constructor<TFilter>): TFilter[];
19
+ export declare function filterByType<Elements, Filter extends Elements>(array: Elements[], filterType: Constructor<Filter>): Filter[];
20
+ /** @deprecated use {@link filterByType} */
21
+ export declare const instanceOf: typeof filterByType;
@@ -1,17 +1,29 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.instanceOf = exports.isConstructor = void 0;
3
+ exports.instanceOf = exports.filterByType = exports.isConstructor = void 0;
4
4
  const isConstructor = (value) => {
5
5
  return !!value && !!value.prototype && !!value.prototype.constructor;
6
6
  };
7
7
  exports.isConstructor = isConstructor;
8
8
  /**
9
- * TODO is this even useful?
10
- * Not sure why I included it, it seems more likely that it's related to
11
- * wanting [this](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards)?
9
+ * ```ts
10
+ * class A {}
11
+ * class B {}
12
+ * class C extends A {}
13
+ *
14
+ * const a = new A();
15
+ * const b = new B();
16
+ * const c = new C();
17
+ *
18
+ * const list = [a, b, c];
19
+ * const filteredList = instanceOf(list, A);
20
+ * // filteredList = [a, c]
21
+ * ```
12
22
  * @ see https://stackoverflow.com/a/65152869/294171
13
23
  */
14
- function instanceOf(array, filterType) {
24
+ function filterByType(array, filterType) {
15
25
  return array.filter((e) => e instanceof filterType);
16
26
  }
17
- exports.instanceOf = instanceOf;
27
+ exports.filterByType = filterByType;
28
+ /** @deprecated use {@link filterByType} */
29
+ exports.instanceOf = filterByType;
package/dist/Mixin.d.ts CHANGED
@@ -1 +1,2 @@
1
+ /** @deprecated see {@link https://www.typescriptlang.org/docs/handbook/mixins.html#constrained-mixins TypeScript docs} */
1
2
  export type Mixin<T extends (...args: any[]) => any> = InstanceType<ReturnType<T>>;
@@ -1,10 +1,40 @@
1
+ /**
2
+ * @see {@link https://stackoverflow.com/a/49725198/294171 StackOverflow response} on requiring at least one property
3
+ */
1
4
  export type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
2
5
  [K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
3
6
  }[Keys];
7
+ /**
8
+ * @see {@link https://stackoverflow.com/a/49725198/294171 StackOverflow response} on requiring only one property
9
+ */
4
10
  export type RequireOnlyOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
5
11
  [K in Keys]-?: Required<Pick<T, K>> & Partial<Record<Exclude<Keys, K>, undefined>>;
6
12
  }[Keys];
7
- /** @see https://stackoverflow.com/a/51365037 */
13
+ /** @see {@link https://stackoverflow.com/a/51365037 StackOverflow response} */
8
14
  export type RecursivePartial<T> = {
9
15
  [P in keyof T]?: T[P] extends (infer U)[] ? RecursivePartial<U>[] : T[P] extends object | undefined ? RecursivePartial<T[P]> : T[P];
10
16
  };
17
+ /**
18
+ * ```ts
19
+ * type A = {
20
+ * d: number,
21
+ * e: string
22
+ * f: boolean
23
+ * }
24
+ *
25
+ * type B = ReplaceProperty<A, 'e', number[]>;
26
+ * // type B = {
27
+ * // d: number,
28
+ * // e: number[],
29
+ * // f: boolean
30
+ * // }
31
+ * ```
32
+ * @see {@link https://stackoverflow.com/a/51599774 StackOverflow response}
33
+ */
34
+ export type ReplaceProperty<T, K extends keyof T, TReplace> = Identity<Pick<T, Exclude<keyof T, K>> & {
35
+ [P in K]: TReplace;
36
+ }>;
37
+ type Identity<T> = {
38
+ [P in keyof T]: T[P];
39
+ };
40
+ export {};
@@ -1,7 +1,10 @@
1
1
  export type AsynchronousFunction = () => Promise<any>;
2
+ /** @deprecated `null` is part of type unless using utility type `Nonnullable` */
2
3
  export type Nullable<T> = T | null;
4
+ /** @deprecated use `?` */
3
5
  export type Optional<T> = T | undefined;
4
6
  export type Subset<T, U extends T> = T;
7
+ /** @deprecated use `Record<string,T>` */
5
8
  export type AssociativeArray<T> = {
6
9
  [key: string]: T;
7
10
  };
@@ -1 +1,23 @@
1
+ /**
2
+
3
+ @see {@link https://stackoverflow.com/a/69571314/294171 StackOverflow response} on static interfaces
4
+
5
+ ```ts
6
+ interface InstanceInterface {
7
+ instanceMethod();
8
+ }
9
+
10
+ interface StaticInterface {
11
+ new(...args: any[]): InstanceInterface;
12
+ staticMethod();
13
+ }
14
+
15
+ class MyClass implements StaticImplements<StaticInterface, typeof MyClass> {
16
+ static staticMethod() { }
17
+ static ownStaticMethod() { }
18
+ instanceMethod() { }
19
+ ownInstanceMethod() { }
20
+ }
21
+ ```
22
+ */
1
23
  export type StaticImplements<I extends new (...args: any[]) => any, C extends I> = InstanceType<C>;
@@ -1,25 +1,2 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- /*
4
-
5
- https://stackoverflow.com/a/69571314/294171
6
-
7
- Usage:
8
-
9
- interface InstanceInterface {
10
- instanceMethod();
11
- }
12
-
13
- interface StaticInterface {
14
- new(...args: any[]): InstanceInterface;
15
- staticMethod();
16
- }
17
-
18
- class MyClass implements StaticImplements<StaticInterface, typeof MyClass> {
19
- static staticMethod() { }
20
- static ownStaticMethod() { }
21
- instanceMethod() { }
22
- ownInstanceMethod() { }
23
- }
24
-
25
- */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@battis/typescript-tricks",
3
3
  "description": "A handful of useful types and operators",
4
- "version": "0.3.0",
4
+ "version": "0.4.1",
5
5
  "author": "Seth Battis <seth@battis.net>",
6
6
  "license": "GPL-3.0",
7
7
  "main": "./dist/index.js",