serverless-spy 0.0.61 → 0.0.62
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/.jsii +3 -2
- package/_config.yml +14 -3
- package/assets/images/site-logo.png +0 -0
- package/dist/releasetag.txt +1 -1
- package/extension/interceptor.ts +5 -1
- package/lib/extension/dist/layer/nodejs/node_modules/interceptor.js +147 -2
- package/lib/extension/dist/layer/nodejs/node_modules/interceptor.js.map +4 -4
- package/lib/src/ServerlessSpy.js +1 -1
- package/logo/full_logo.png +0 -0
- package/node_modules/serialize-error/error-constructors.d.ts +8 -0
- package/node_modules/serialize-error/error-constructors.js +26 -0
- package/node_modules/serialize-error/index.d.ts +171 -0
- package/node_modules/serialize-error/index.js +205 -0
- package/node_modules/serialize-error/license +9 -0
- package/node_modules/serialize-error/node_modules/type-fest/index.d.ts +95 -0
- package/node_modules/serialize-error/node_modules/type-fest/package.json +52 -0
- package/node_modules/serialize-error/node_modules/type-fest/readme.md +905 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/async-return-type.d.ts +25 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/asyncify.d.ts +33 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/basic.d.ts +45 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/camel-case.d.ts +73 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/camel-cased-properties-deep.d.ts +54 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/camel-cased-properties.d.ts +36 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/conditional-except.d.ts +45 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/conditional-keys.d.ts +47 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/conditional-pick.d.ts +44 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/delimiter-case.d.ts +93 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/delimiter-cased-properties-deep.d.ts +60 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/delimiter-cased-properties.d.ts +37 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/entries.d.ts +62 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/entry.d.ts +65 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/exact.d.ts +73 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/except.d.ts +57 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/fixed-length-array.d.ts +43 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/get.d.ts +184 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/has-optional-keys.d.ts +21 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/has-required-keys.d.ts +59 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/includes.d.ts +22 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/internal.d.ts +59 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/invariant-of.d.ts +76 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/iterable-element.d.ts +54 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/join.d.ts +30 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/jsonify.d.ts +90 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/kebab-case.d.ts +38 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/kebab-cased-properties-deep.d.ts +47 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/kebab-cased-properties.d.ts +30 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/last-array-element.d.ts +28 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/literal-to-primitive.d.ts +36 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/literal-union.d.ts +35 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/merge-exclusive.d.ts +41 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/merge.d.ts +27 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/multidimensional-array.d.ts +43 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/multidimensional-readonly-array.d.ts +47 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/mutable.d.ts +5 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/numeric.d.ts +170 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/observable-like.d.ts +62 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/opaque.d.ts +107 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/optional-keys-of.d.ts +38 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/package-json.d.ts +663 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/partial-deep.d.ts +113 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/partial-on-undefined-deep.d.ts +70 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/pascal-case.d.ts +38 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/pascal-cased-properties-deep.d.ts +54 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/pascal-cased-properties.d.ts +34 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/primitive.d.ts +13 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/promisable.d.ts +25 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/promise-value.d.ts +29 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/readonly-deep.d.ts +85 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/readonly-tuple.d.ts +41 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/remove-index-signature.d.ts +104 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/replace.d.ts +67 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/require-all-or-none.d.ts +36 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/require-at-least-one.d.ts +35 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/require-exactly-one.d.ts +34 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/required-keys-of.d.ts +29 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/schema.d.ts +72 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/screaming-snake-case.d.ts +33 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/set-non-nullable.d.ts +35 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/set-optional.d.ts +35 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/set-required.d.ts +35 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/set-return-type.d.ts +31 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/simplify.d.ts +83 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/snake-case.d.ts +38 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/snake-cased-properties-deep.d.ts +47 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/snake-cased-properties.d.ts +30 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/split.d.ts +29 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/spread.d.ts +85 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/string-key-of.d.ts +25 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/stringified.d.ts +23 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/trim.d.ts +25 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/tsconfig-json.d.ts +1172 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/typed-array.d.ts +17 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/union-to-intersection.d.ts +60 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/value-of.d.ts +42 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/writable.d.ts +40 -0
- package/node_modules/serialize-error/package.json +46 -0
- package/node_modules/serialize-error/readme.md +198 -0
- package/package.json +6 -4
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
type Numeric = number | bigint;
|
|
2
|
+
|
|
3
|
+
type Zero = 0 | 0n;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
Matches the hidden `Infinity` type.
|
|
7
|
+
|
|
8
|
+
Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277) if you want to have this type as a built-in in TypeScript.
|
|
9
|
+
|
|
10
|
+
@see NegativeInfinity
|
|
11
|
+
|
|
12
|
+
@category Numeric
|
|
13
|
+
*/
|
|
14
|
+
// See https://github.com/microsoft/TypeScript/issues/31752
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
|
|
16
|
+
export type PositiveInfinity = 1e999;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
Matches the hidden `-Infinity` type.
|
|
20
|
+
|
|
21
|
+
Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277) if you want to have this type as a built-in in TypeScript.
|
|
22
|
+
|
|
23
|
+
@see PositiveInfinity
|
|
24
|
+
|
|
25
|
+
@category Numeric
|
|
26
|
+
*/
|
|
27
|
+
// See https://github.com/microsoft/TypeScript/issues/31752
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
|
|
29
|
+
export type NegativeInfinity = -1e999;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
A finite `number`.
|
|
33
|
+
You can't pass a `bigint` as they are already guaranteed to be finite.
|
|
34
|
+
|
|
35
|
+
Use-case: Validating and documenting parameters.
|
|
36
|
+
|
|
37
|
+
Note: This can't detect `NaN`, please upvote [this issue](https://github.com/microsoft/TypeScript/issues/28682) if you want to have this type as a built-in in TypeScript.
|
|
38
|
+
|
|
39
|
+
@example
|
|
40
|
+
```
|
|
41
|
+
import type {Finite} from 'type-fest';
|
|
42
|
+
|
|
43
|
+
declare function setScore<T extends number>(length: Finite<T>): void;
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
@category Numeric
|
|
47
|
+
*/
|
|
48
|
+
export type Finite<T extends number> = T extends PositiveInfinity | NegativeInfinity ? never : T;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
A `number` that is an integer.
|
|
52
|
+
You can't pass a `bigint` as they are already guaranteed to be integers.
|
|
53
|
+
|
|
54
|
+
Use-case: Validating and documenting parameters.
|
|
55
|
+
|
|
56
|
+
@example
|
|
57
|
+
```
|
|
58
|
+
import type {Integer} from 'type-fest';
|
|
59
|
+
|
|
60
|
+
declare function setYear<T extends number>(length: Integer<T>): void;
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
@see NegativeInteger
|
|
64
|
+
@see NonNegativeInteger
|
|
65
|
+
|
|
66
|
+
@category Numeric
|
|
67
|
+
*/
|
|
68
|
+
// `${bigint}` is a type that matches a valid bigint literal without the `n` (ex. 1, 0b1, 0o1, 0x1)
|
|
69
|
+
// Because T is a number and not a string we can effectively use this to filter out any numbers containing decimal points
|
|
70
|
+
export type Integer<T extends number> = `${T}` extends `${bigint}` ? T : never;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
A `number` that is not an integer.
|
|
74
|
+
You can't pass a `bigint` as they are already guaranteed to be integers.
|
|
75
|
+
|
|
76
|
+
Use-case: Validating and documenting parameters.
|
|
77
|
+
|
|
78
|
+
@example
|
|
79
|
+
```
|
|
80
|
+
import type {Float} from 'type-fest';
|
|
81
|
+
|
|
82
|
+
declare function setPercentage<T extends number>(length: Float<T>): void;
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
@see Integer
|
|
86
|
+
|
|
87
|
+
@category Numeric
|
|
88
|
+
*/
|
|
89
|
+
export type Float<T extends number> = T extends Integer<T> ? never : T;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
A negative (`-∞ < x < 0`) `number` that is not an integer.
|
|
93
|
+
Equivalent to `Negative<Float<T>>`.
|
|
94
|
+
|
|
95
|
+
Use-case: Validating and documenting parameters.
|
|
96
|
+
|
|
97
|
+
@see Negative
|
|
98
|
+
@see Float
|
|
99
|
+
|
|
100
|
+
@category Numeric
|
|
101
|
+
*/
|
|
102
|
+
export type NegativeFloat<T extends number> = Negative<Float<T>>;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
A negative `number`/`bigint` (`-∞ < x < 0`)
|
|
106
|
+
|
|
107
|
+
Use-case: Validating and documenting parameters.
|
|
108
|
+
|
|
109
|
+
@see NegativeInteger
|
|
110
|
+
@see NonNegative
|
|
111
|
+
|
|
112
|
+
@category Numeric
|
|
113
|
+
*/
|
|
114
|
+
export type Negative<T extends Numeric> = T extends Zero ? never : `${T}` extends `-${string}` ? T : never;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
A negative (`-∞ < x < 0`) `number` that is an integer.
|
|
118
|
+
Equivalent to `Negative<Integer<T>>`.
|
|
119
|
+
|
|
120
|
+
You can't pass a `bigint` as they are already guaranteed to be integers, instead use `Negative<T>`.
|
|
121
|
+
|
|
122
|
+
Use-case: Validating and documenting parameters.
|
|
123
|
+
|
|
124
|
+
@see Negative
|
|
125
|
+
@see Integer
|
|
126
|
+
|
|
127
|
+
@category Numeric
|
|
128
|
+
*/
|
|
129
|
+
export type NegativeInteger<T extends number> = Negative<Integer<T>>;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
A non-negative `number`/`bigint` (`0 <= x < ∞`).
|
|
133
|
+
|
|
134
|
+
Use-case: Validating and documenting parameters.
|
|
135
|
+
|
|
136
|
+
@see NonNegativeInteger
|
|
137
|
+
@see Negative
|
|
138
|
+
|
|
139
|
+
@example
|
|
140
|
+
```
|
|
141
|
+
import type {NonNegative} from 'type-fest';
|
|
142
|
+
|
|
143
|
+
declare function setLength<T extends number>(length: NonNegative<T>): void;
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
@category Numeric
|
|
147
|
+
*/
|
|
148
|
+
export type NonNegative<T extends Numeric> = T extends Zero ? T : Negative<T> extends never ? T : never;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
A non-negative (`0 <= x < ∞`) `number` that is an integer.
|
|
152
|
+
Equivalent to `NonNegative<Integer<T>>`.
|
|
153
|
+
|
|
154
|
+
You can't pass a `bigint` as they are already guaranteed to be integers, instead use `NonNegative<T>`.
|
|
155
|
+
|
|
156
|
+
Use-case: Validating and documenting parameters.
|
|
157
|
+
|
|
158
|
+
@see NonNegative
|
|
159
|
+
@see Integer
|
|
160
|
+
|
|
161
|
+
@example
|
|
162
|
+
```
|
|
163
|
+
import type {NonNegativeInteger} from 'type-fest';
|
|
164
|
+
|
|
165
|
+
declare function setLength<T extends number>(length: NonNegativeInteger<T>): void;
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
@category Numeric
|
|
169
|
+
*/
|
|
170
|
+
export type NonNegativeInteger<T extends number> = NonNegative<Integer<T>>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
interface SymbolConstructor {
|
|
3
|
+
readonly observable: symbol;
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
@remarks
|
|
9
|
+
The TC39 observable proposal defines a `closed` property, but some implementations (such as xstream) do not as of 10/08/2021.
|
|
10
|
+
As well, some guideance on making an `Observable` do not include `closed` propery.
|
|
11
|
+
@see https://github.com/tc39/proposal-observable/blob/master/src/Observable.js#L129-L130
|
|
12
|
+
@see https://github.com/staltz/xstream/blob/6c22580c1d84d69773ee4b0905df44ad464955b3/src/index.ts#L79-L85
|
|
13
|
+
@see https://github.com/benlesh/symbol-observable#making-an-object-observable
|
|
14
|
+
|
|
15
|
+
@category Observable
|
|
16
|
+
*/
|
|
17
|
+
export type Unsubscribable = {
|
|
18
|
+
unsubscribe(): void;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
@category Observable
|
|
23
|
+
*/
|
|
24
|
+
type OnNext<ValueType> = (value: ValueType) => void;
|
|
25
|
+
/**
|
|
26
|
+
@category Observable
|
|
27
|
+
*/
|
|
28
|
+
type OnError = (error: unknown) => void;
|
|
29
|
+
/**
|
|
30
|
+
@category Observable
|
|
31
|
+
*/
|
|
32
|
+
type OnComplete = () => void;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
@category Observable
|
|
36
|
+
*/
|
|
37
|
+
export type Observer<ValueType> = {
|
|
38
|
+
next: OnNext<ValueType>;
|
|
39
|
+
error: OnError;
|
|
40
|
+
complete: OnComplete;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable).
|
|
45
|
+
|
|
46
|
+
@remarks
|
|
47
|
+
The TC39 Observable proposal defines 2 forms of `subscribe()`:
|
|
48
|
+
1. Three callback arguments: `subscribe(observer: OnNext<ValueType>, onError?: OnError, onComplete?: OnComplete): Unsubscribable;`
|
|
49
|
+
2. A single `observer` argument: (as defined below)
|
|
50
|
+
|
|
51
|
+
But `Observable` implementations have evolved to preferring case 2 and some implementations choose not to implement case 1. Therefore, an `ObservableLike` cannot be trusted to implement the first case. (xstream and hand built observerables often do not implement case 1)
|
|
52
|
+
|
|
53
|
+
@see https://github.com/tc39/proposal-observable#observable
|
|
54
|
+
@see https://github.com/tc39/proposal-observable/blob/master/src/Observable.js#L246-L259
|
|
55
|
+
@see https://benlesh.com/posts/learning-observable-by-building-observable/
|
|
56
|
+
|
|
57
|
+
@category Observable
|
|
58
|
+
*/
|
|
59
|
+
export interface ObservableLike<ValueType = unknown> {
|
|
60
|
+
subscribe(observer?: Partial<Observer<ValueType>>): Unsubscribable;
|
|
61
|
+
[Symbol.observable](): ObservableLike<ValueType>;
|
|
62
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
declare const tag: unique symbol;
|
|
2
|
+
|
|
3
|
+
declare type Tagged<Token> = {
|
|
4
|
+
readonly [tag]: Token;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
Create an opaque type, which hides its internal details from the public, and can only be created by being used explicitly.
|
|
9
|
+
|
|
10
|
+
The generic type parameter can be anything. It doesn't have to be an object.
|
|
11
|
+
|
|
12
|
+
[Read more about opaque types.](https://codemix.com/opaque-types-in-javascript/)
|
|
13
|
+
|
|
14
|
+
There have been several discussions about adding this feature to TypeScript via the `opaque type` operator, similar to how Flow does it. Unfortunately, nothing has (yet) moved forward:
|
|
15
|
+
- [Microsoft/TypeScript#202](https://github.com/microsoft/TypeScript/issues/202)
|
|
16
|
+
- [Microsoft/TypeScript#15408](https://github.com/Microsoft/TypeScript/issues/15408)
|
|
17
|
+
- [Microsoft/TypeScript#15807](https://github.com/Microsoft/TypeScript/issues/15807)
|
|
18
|
+
|
|
19
|
+
@example
|
|
20
|
+
```
|
|
21
|
+
import type {Opaque} from 'type-fest';
|
|
22
|
+
|
|
23
|
+
type AccountNumber = Opaque<number, 'AccountNumber'>;
|
|
24
|
+
type AccountBalance = Opaque<number, 'AccountBalance'>;
|
|
25
|
+
|
|
26
|
+
// The `Token` parameter allows the compiler to differentiate between types, whereas "unknown" will not. For example, consider the following structures:
|
|
27
|
+
type ThingOne = Opaque<string>;
|
|
28
|
+
type ThingTwo = Opaque<string>;
|
|
29
|
+
|
|
30
|
+
// To the compiler, these types are allowed to be cast to each other as they have the same underlying type. They are both `string & { __opaque__: unknown }`.
|
|
31
|
+
// To avoid this behaviour, you would instead pass the "Token" parameter, like so.
|
|
32
|
+
type NewThingOne = Opaque<string, 'ThingOne'>;
|
|
33
|
+
type NewThingTwo = Opaque<string, 'ThingTwo'>;
|
|
34
|
+
|
|
35
|
+
// Now they're completely separate types, so the following will fail to compile.
|
|
36
|
+
function createNewThingOne (): NewThingOne {
|
|
37
|
+
// As you can see, casting from a string is still allowed. However, you may not cast NewThingOne to NewThingTwo, and vice versa.
|
|
38
|
+
return 'new thing one' as NewThingOne;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// This will fail to compile, as they are fundamentally different types.
|
|
42
|
+
const thingTwo = createNewThingOne() as NewThingTwo;
|
|
43
|
+
|
|
44
|
+
// Here's another example of opaque typing.
|
|
45
|
+
function createAccountNumber(): AccountNumber {
|
|
46
|
+
return 2 as AccountNumber;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function getMoneyForAccount(accountNumber: AccountNumber): AccountBalance {
|
|
50
|
+
return 4 as AccountBalance;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// This will compile successfully.
|
|
54
|
+
getMoneyForAccount(createAccountNumber());
|
|
55
|
+
|
|
56
|
+
// But this won't, because it has to be explicitly passed as an `AccountNumber` type.
|
|
57
|
+
getMoneyForAccount(2);
|
|
58
|
+
|
|
59
|
+
// You can use opaque values like they aren't opaque too.
|
|
60
|
+
const accountNumber = createAccountNumber();
|
|
61
|
+
|
|
62
|
+
// This will not compile successfully.
|
|
63
|
+
const newAccountNumber = accountNumber + 2;
|
|
64
|
+
|
|
65
|
+
// As a side note, you can (and should) use recursive types for your opaque types to make them stronger and hopefully easier to type.
|
|
66
|
+
type Person = {
|
|
67
|
+
id: Opaque<number, Person>;
|
|
68
|
+
name: string;
|
|
69
|
+
};
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
@category Type
|
|
73
|
+
*/
|
|
74
|
+
export type Opaque<Type, Token = unknown> = Type & Tagged<Token>;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
Revert an opaque type back to its original type by removing the readonly `[tag]`.
|
|
78
|
+
|
|
79
|
+
Why is this necessary?
|
|
80
|
+
|
|
81
|
+
1. Use an `Opaque` type as object keys
|
|
82
|
+
2. Prevent TS4058 error: "Return type of exported function has or is using name X from external module Y but cannot be named"
|
|
83
|
+
|
|
84
|
+
@example
|
|
85
|
+
```
|
|
86
|
+
import type {Opaque, UnwrapOpaque} from 'type-fest';
|
|
87
|
+
|
|
88
|
+
type AccountType = Opaque<'SAVINGS' | 'CHECKING', 'AccountType'>;
|
|
89
|
+
|
|
90
|
+
const moneyByAccountType: Record<UnwrapOpaque<AccountType>, number> = {
|
|
91
|
+
SAVINGS: 99,
|
|
92
|
+
CHECKING: 0.1
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// Without UnwrapOpaque, the following expression would throw a type error.
|
|
96
|
+
const money = moneyByAccountType.SAVINGS; // TS error: Property 'SAVINGS' does not exist
|
|
97
|
+
|
|
98
|
+
// Attempting to pass an non-Opaque type to UnwrapOpaque will raise a type error.
|
|
99
|
+
type WontWork = UnwrapOpaque<string>;
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
@category Type
|
|
103
|
+
*/
|
|
104
|
+
export type UnwrapOpaque<OpaqueType extends Tagged<unknown>> =
|
|
105
|
+
OpaqueType extends Opaque<infer Type, OpaqueType[typeof tag]>
|
|
106
|
+
? Type
|
|
107
|
+
: OpaqueType;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Extract all optional keys from the given type.
|
|
3
|
+
|
|
4
|
+
This is useful when you want to create a new type that contains different type values for the optional keys only.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import type {OptionalKeysOf, Except} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
interface User {
|
|
11
|
+
name: string;
|
|
12
|
+
surname: string;
|
|
13
|
+
|
|
14
|
+
luckyNumber?: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const REMOVE_FIELD = Symbol('remove field symbol');
|
|
18
|
+
type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {
|
|
19
|
+
[Key in OptionalKeysOf<Entity>]?: Entity[Key] | typeof REMOVE_FIELD;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const update1: UpdateOperation<User> = {
|
|
23
|
+
name: 'Alice'
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const update2: UpdateOperation<User> = {
|
|
27
|
+
name: 'Bob',
|
|
28
|
+
luckyNumber: REMOVE_FIELD
|
|
29
|
+
};
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
@category Utilities
|
|
33
|
+
*/
|
|
34
|
+
export type OptionalKeysOf<BaseType extends object> = Exclude<{
|
|
35
|
+
[Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]>
|
|
36
|
+
? never
|
|
37
|
+
: Key
|
|
38
|
+
}[keyof BaseType], undefined>;
|