Last active
February 7, 2024 15:35
-
-
Save wanderer20/104b90810bb9a481a7e3f73f13278a58 to your computer and use it in GitHub Desktop.
Пользовательская защита или проверка пользовательского типа в TypeScript. Идея - использовать дискриминатор, как точный идентификатор для типа, дабы четко совершать проверку
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type CustomType = { | |
discriminator: 'customType'; | |
field1: number; | |
field2: string; | |
} | |
... | |
const isCustomType = (object: any): object is CustomType => !!object && object.discriminator === 'customType'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment