-
Notifications
You must be signed in to change notification settings - Fork 89
Add 35-mapping-modifiers #305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add 35-mapping-modifiers #305
Conversation
|
||
Чтобы добавить или удалить эти модификаторы, можно использовать префиксы `+` или `-`. Если не использовать префикс, то подразумевается что модификатор будет добавлен, то есть по умолчанию префикс `+`. | ||
|
||
Примеры использования модификаторов есть в Utility Types: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Давай подумаем над примером из реальной жизни
Подобным образом можно написать и тип, который делает все свойства типа изменяемыми, то есть удаляет атрибут `readonly`: | ||
|
||
```typescript | ||
type Mutable<T> = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
давай также покажем как используется
-readonly [P in keyof T]: T[P]; | ||
}; | ||
``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
нужно добавить небольшой "рекап" - что мы изучили и как оно нам может пригодиться
const deepFreeze = <T extends object>(obj: T): DeepReadonly<T> => { | ||
Object.freeze(obj); | ||
|
||
Object.values(obj).forEach((value) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
мы стараемся писать имутабельный код
@@ -0,0 +1,19 @@ | |||
// BEGIN | |||
type DeepReadonly<T> = { | |||
readonly [P in keyof T]: T[P] extends object ? DeepReadonly<T[P]> : T[P]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а что делать с массивами?
@@ -0,0 +1,14 @@ | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
лишняя строчка
}, | ||
}; | ||
|
||
expect(deepFreeze(obj)).toEqual({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
хорошо бы проверить что также сама логика валидная и мы не можем поменять свойства
3058301
to
b4e8abe
Compare
No description provided.