-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_validation_inference_2.ts
35 lines (26 loc) · 1022 Bytes
/
2_validation_inference_2.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
type ErrorMessage<Text extends string | number> =
`Property [${Text}] is forbidden`
// Remove it and write from scratch
type ForbidProperty<
Obj extends Record<string, unknown>,
ForbiddenProperty extends string | number
> = {
[Prop in keyof Obj]:
Prop extends ForbiddenProperty
? ErrorMessage<ForbiddenProperty> // replace with error message
: Obj[Prop] // leave as is
}
// We want to forbid using [name]
const foo = <
Prop extends PropertyKey,
Value extends Json,
Obj extends Record<Prop, Value>
>(obj: ForbidProperty<Obj, 'name'>) => obj
// 1) try any property with underscore
// 2) disallow using of error message as a value
foo({ email: '[email protected]', name: 'John' })
// --------------> HOME WORK <--------------
// Throw TS error if any value in provided argument is 5
foo({ age: 5 }) // expect error
foo({ age: 6 }) // ok
// You can find more about validations here https://catchts.com/validators and here https://catchts.com/type-negation