Union of generics

I've tried to write the following :
export const arkQueryCriteriaValue = type('<Value>', [
{ eq: 'Value' },
'|',
{ notEq: 'Value' },
'|',
{ in: 'Value[]' },
]);
export const arkQueryCriteriaValue = type('<Value>', [
{ eq: 'Value' },
'|',
{ notEq: 'Value' },
'|',
{ in: 'Value[]' },
]);
But unfortunately it doesn't work because the 2nd parameter accepts a maximum of 3 elements. Is there a way to add arbitrary number of element to a union issued from a generic ?
17 Replies
ssalbdivad
ssalbdivad6mo ago
Tuple expressions are binary so you'd have to nest them.
JesusTheHun
JesusTheHunOP6mo ago
Oh, I'm gonna try that It works. It is not very readable though. Is there any piece of doc to learn how to write functions that take a Type as parameter ? I've tried a few things and my function generics could never capture the true type of the given arguments. This way I could write a function that just build the tuple tower for me ^^
Dimava
Dimava6mo ago
Btw maybe you want +:reject AT allows extra props by default
JesusTheHun
JesusTheHunOP6mo ago
I want +:reject ? you mean to strip additionnal properties ?
Dimava
Dimava6mo ago
Yeah what you gonna do on { eq, notEq } May be fine depending on what you do but generally nt
JesusTheHun
JesusTheHunOP6mo ago
I'm gonna do specific specs, no prop iteration so it should be fine, but if I can strip extra props I'm all for it
ssalbdivad
ssalbdivad6mo ago
{"+": "delete" }if you add a key like that to your object it will behave that way
Dimava
Dimava6mo ago
I mean it may be not A|B but A&B, and if you don't handle that you should reject that What will you do with
let validFoo = { eq: 1, notEq: 1 }
let validFoo = { eq: 1, notEq: 1 }
?
JesusTheHun
JesusTheHunOP6mo ago
So like this ?
type({
foo: 'string',
'+': 'delete'
})
type({
foo: 'string',
'+': 'delete'
})
Dimava
Dimava6mo ago
I don't generally recommend delete, consider reject
ssalbdivad
ssalbdivad6mo ago
It's just a question of whether you want the validation to fail or you want the extra keys stripped
Dimava
Dimava6mo ago
For unions I mean Does delete fail on multibranch union?
ssalbdivad
ssalbdivad6mo ago
Yeah ideally the type would fail to compile if it has delete and the union is not discriminatable like in this case
JesusTheHun
JesusTheHunOP6mo ago
Pick whatever comes first in my switch ^^ it's an internal API so I the end-dev has basic knowledge of the API. But yes, you are right, I would prefer the request to be rejected. With '+': 'reject' it will throw if there is more prop that the member of the union allow ? That's sick 😍 how can you add that option to a type after the declaration ? Like myArkValidator.options({ reject: true )
ssalbdivad
ssalbdivad6mo ago
onUndeclaredKey
JesusTheHun
JesusTheHunOP6mo ago
gtg, will be back tonight
ssalbdivad
ssalbdivad6mo ago
.onUndeclaredKey

Did you find this page helpful?