/
common-types.ts
116 lines (101 loc) · 2.54 KB
/
common-types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright IBM Corp. 2017,2019. All Rights Reserved.
// Node module: @loopback/repository
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
/**
* Common types/interfaces such as Class/Constructor/Options/Callback
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Interface for classes with `new` operator and static properties/methods
*/
export interface Class<T> {
// new MyClass(...args) ==> T
new (...args: any[]): T;
// Other static properties/operations
[property: string]: any;
}
/**
* Interface for constructor functions without `new` operator.
*
* @example
* ```ts
* function Foo(x) {
* if (!(this instanceof Foo)) { return new Foo(x); }
* this.x = x;
* }
* ```
*/
export interface ConstructorFunction<T> {
(...args: any[]): T;
}
/**
* Constructor type - class or function
*/
export type Constructor<T> = Class<T> | ConstructorFunction<T>;
/**
* Objects with open properties
*/
export interface AnyObject {
[property: string]: any;
}
/**
* An extension of the built-in Partial<T> type which allows partial values
* in deeply nested properties too.
*/
export type DeepPartial<T> =
| Partial<T> // handle free-form properties, e.g. DeepPartial<AnyObject>
| {[P in keyof T]?: DeepPartial<T[P]>};
/**
* Type alias for strongly or weakly typed objects of T
*/
export type DataObject<T extends object> = T | DeepPartial<T>;
/**
* Type alias for Node.js options object
*/
export type Options = AnyObject;
/**
* Type alias for Node.js callback functions
*/
export type Callback<T> = (
err: Error | string | null | undefined,
result?: T,
) => void;
/**
* Type for a command
*/
export type Command = string | AnyObject;
/**
* Named parameters, such as `{x: 1, y: 'a'}`
*/
export type NamedParameters = AnyObject;
/**
* Positional parameters, such as [1, 'a']
*/
export type PositionalParameters = any[];
/**
* Count of Model instances that were successful for methods like `updateAll`,
* `deleteAll`, etc.
*/
export interface Count {
count: number;
}
/**
* JSON Schema describing the Count interface. It's the response type for
* REST calls to APIs which return Count
*/
export const CountSchema = {
type: 'object',
title: 'Loopback.Count',
properties: {count: {type: 'number'}},
};
/**
* Type helper to infer prototype from a constructor function.
*
* Example: `PrototypeOf<typeof Entity>` is resolved to `Entity`.
*/
export type PrototypeOf<Ctor extends Function> = Ctor extends {
prototype: infer Proto;
}
? Proto
: never;