You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It works by using an object intersection with a primitive. Since objects can be intersected with anything, this does not create a never type, but instead the type expects the primitive, and since objects can't be primitives, only the primitive type is allowed to be assigned. Since the object type information is still part of the type, those branded or flavored primitives cannot then be reassigned to another type, achieving a type that acts like a nominal type.
Among other things, this feature is very helpful for creating id types that cannot accidentally be misassigned. We use this feature for our ids to gain additional type safety so we do not accidentally use the wrong kind of id for a query that does not expect it.
Typescript-is does not seem to support this feature, and incorrectly expects there to be an object as opposed to the primitive. This seems to be an error with how intersections are parsed when they are paired with a primitive.
will fail with the error: TypeGuardError: validation failed at $: expected an object, found: 'person_id_abcd' expecting the input to be an object, however, this behavior does not match the behavior of typescript:
The typescript compiler expects PersonId to be a string, and attempting to assign an object to it should result in an error, meaning the behavior of typescript-is in this specific scenario is invalid.
The text was updated successfully, but these errors were encountered:
"Branding" and "Flavoring" is a technique to achieve nominal like typing in typescript
It works by using an object intersection with a primitive. Since objects can be intersected with anything, this does not create a never type, but instead the type expects the primitive, and since objects can't be primitives, only the primitive type is allowed to be assigned. Since the object type information is still part of the type, those branded or flavored primitives cannot then be reassigned to another type, achieving a type that acts like a nominal type.
Among other things, this feature is very helpful for creating id types that cannot accidentally be misassigned. We use this feature for our ids to gain additional type safety so we do not accidentally use the wrong kind of id for a query that does not expect it.
Typescript-is does not seem to support this feature, and incorrectly expects there to be an object as opposed to the primitive. This seems to be an error with how intersections are parsed when they are paired with a primitive.
Currently, creating an assertion like:
will fail with the error:
TypeGuardError: validation failed at $: expected an object, found: 'person_id_abcd'
expecting the input to be an object, however, this behavior does not match the behavior of typescript:The typescript compiler expects
PersonId
to be a string, and attempting to assign an object to it should result in an error, meaning the behavior of typescript-is in this specific scenario is invalid.The text was updated successfully, but these errors were encountered: