typia: Nominal typing support (nonsensible intersection error)
Question
Hi all, I’m trying to migrate from class-validator/class-transformer to typia and nestia, but I’m facing some issue that don’t know how yo fix, or wether is expected or not.
I have an auxiliar type to create “kinf of” nominal typing
declare type Nominal<Type, Identifier> = Type & {
readonly [__nominal__type]: Identifier;
};
export type UserID = Nominal<string, 'UserID'>;
In my DTO i use it like this
export interface TestDTO {
@ApiProperty({ type: String, format: 'uuid' })
@IsUUID()
userId: userId;
}
when I remove old anotations and try to use typia and nestia I’m seeing the following error (when running a typescript check)
error TS(@nestia.core.TypedBody): unsupported type detected
- CreateRecommendationDTO.memberId: string & __type
- nonsensible intersection
I found this PR that seems that this should be supported.
any clue why this is happening ?
thanks
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 43 (17 by maintainers)
For those of us who’s projects aren’t “low important things”, or “have time to waste”, I have reapplied my patch to 7.0.1
Drop in install:
Usage:
The
typia.tags.NoRuntimeChecksimply suppresses the field check for (in this case)brand. It works because it’s always been possible since type v6 to checkprimitive & {}(an empty object). This typia.tag simply makes the object look empty.seems like v7 has been released 🎉 , can you point us the to the change related with this issue? thanks 😃
@MatAtBread Yes, I still think that the reinterpret casting by
asstatement seems not valid.If I needed the discrimination, I just utilzed actual brand type, and it is much type safer.
Whether I like the brand type or not, you are going to support brand type, you need to find a better solution. Looking at your code MatAtBread/typia#1,
NoRuntimeCheckis not a fundamental solution to the brand type what you want, but just avoiding the confliction with currenttypiasystem with hard coding.To resolve this problem clearly, you have to design the priority rule. For example, when
number & object (brand)type comes, you can make a rule thatnumbertakes precedence overobject. Or you can design another rule thatleft side first and right side later. Such rule must be valid even when complicate intersection and union type comes, and there must not be any nonsensible case.I repeat the previous example pseudo code again:
(A | B | C) & (D | E) & (F | G)(((A | B | C) & (D | E) & (F | G)) & X) | ((a | b) & (c & d) & x)@AlexRMU - how do you mean “optional”?
This is only partially true. See https://typia.io/playground/?script=JYWwDg9gTgLgBDAnmYBDOAzKERwERIqp4DcAUGYQKZwBCcAvHAHYCuIARlVHAGRwBvOAH0OUVMwAmAfgBccAOQcFcAL7lqcAMKMW7Lj35DR4qXMUBjFeooZWzCzGARmcAB4AKVPLaduASkFVMjsHJxc4RC95WkCBYNDHZ1cAL2jtOOCyATI4OAsXAGd4Dl0ARgAmAGZyXPcPDn8SODqoxua6tPa4AHoeuAB5CDBCgEI4ADkIOAB3VChmYGYAczIsnLyC5mK4Dll6JkqaijzPdtaGps7L5r64AFEobB4PLWgoKkdR-3lS4EKWBB4OgdGsKBt8kUSuVqnBUADaLVTjdev03k9PjBfnB-nC9H4oBdund0R9HNjcehaNdif1Hs84K93pjvhSAcwgXjQVkgA
…the only “failure” case is
const b = 123;- this can now be passed to anything that expects a branded number irrespective of the brand, because the “brand” is optional.My personal view is a
typia.tag.NoRuntimeCheckwould be ideal as it specifies that a member could be required at compile time, but absent at run-time. This is a rare edge case, as far as I know only useful to this use-case, but it logically solves the problem in a very easy to reason about way. @samchon doesn’t like a run time validator that can optionally not validate, which I completely get, but it doesn’t help solve this problem, which is used in codebases I’m responsible for extensively.