generated from ehmicky/template-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.d.ts
64 lines (61 loc) · 1.34 KB
/
main.d.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
/**
* `set-error-props` options
*/
export interface Options {
/**
* Prevents overriding existing properties.
*
* @default false
*
* @example
* ```js
* const error = new Error('message')
* error.one = true
* setErrorProps(error, { one: false, two: true }, { soft: true })
* console.log(error.one) // true
* console.log(error.two) // true
* ```
*/
readonly soft?: boolean
}
type MergeObjects<Low extends object, High extends object> = High &
Omit<Low, keyof High>
type CoreErrorProps =
| 'name'
| 'message'
| 'stack'
| 'cause'
| 'error'
| 'constructor'
| 'toString'
| 'hasOwnProperty'
| 'isPrototypeOf'
| 'propertyIsEnumerable'
| 'valueOf'
| 'toLocaleString'
/**
* Assigns `props` to `error`.
*
* @example
* ```js
* const error = new Error('message')
* error.prop = { one: [true] }
* setErrorProps(error, { prop: { one: [false], two: true } })
* console.log(error.prop) // { one: [true, false], two: true }
* ```
*/
export default function setErrorProps<
ErrorArg extends object,
Props extends object,
OptionsArg extends Options,
>(
error: ErrorArg,
props: Props,
options?: OptionsArg,
): Pick<ErrorArg, CoreErrorProps & keyof ErrorArg> &
Omit<
OptionsArg['soft'] extends true
? MergeObjects<Props, ErrorArg>
: MergeObjects<ErrorArg, Props>,
CoreErrorProps
>