-
Notifications
You must be signed in to change notification settings - Fork 329
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
A few questions when upgrading from 1.3 to 1.8 #304
Comments
I can't repro, neither with It depends, if for example both
Yes
Yes
const myType = t.exact(t.intersection([
t.type({allMandatoryFields}),
t.partial({allOptionalFields})
]))
I can't repro, neither with |
Thank you for answer 2 and 3, it's very nice that exact can handle intersections! For answer 1 I'll try to keep investigating to check why it is happening, I'll update the issue as soon as I can. for answer 4, the error doesn't appear when I define the brand, but when I try to assign a function parameter to it:
Even using the brand definition in the docs I still have issues:
|
@MastroLindus that's the point of branded types, you MUST pass through the validation otherwise you get a static error declare function foo(bar: t.TypeOf<typeof RecordWithKeysType>): string
const result: Either<t.Errors, string> = RecordWithKeysType.decode({ asd: 'gdf' }).map(foo) See also https://dev.to/gcanti/functional-design-smart-constructors-14nb |
Thank you, now it's clearer. Now if I want to send a branded parameter from the client (let's say a positive integer), I have to still process it trough validation even there, otherwise I will get a static error that won't allow me to proceed. |
Not sure I'm following, maybe a real world example would help. If you type the function foo(bar: Positive) { } that means that you are interested in a static check, that is you want to avoid this kind of errors foo(1) // ok
foo(-1.2) // opss... If you are not interested in such static guarantees, why using |
My setup is like this (server is nodejs-based) The server declares the public API in an array like:
I use this information: Also, at runtime, the server uses that information to validate the parameters passed from the client, before the API method are allowed to run. Obviously they will only run if the validation is successful. Now, in case I want to invoke a method on the server with a branded parameter, I am forced to running through decode also on the client, even if my system guarantees that it will be already checked at runtime on the server. I want to use the branded type to ensure that the those types are valid at runtime, but honestly I don't care so much about the static guarantee, since on the server I cannot trust that the client is legit anyway. TL;DR; I already ensure that all the parameters go through validation at runtime, and I cannot trust the static validation anyway, so having to decode all the branded types at calling time is more of a chore than useful, however I understand the logic behind it and I can live with that |
nice, I did the same in a few fullstack-TS projects and it worked great. The only thing is making sure your route definitions can be imported straight from the client code without any nodejs noise around it :)
@MastroLindus this makes me think that you're not taking the full advantage of branded types on the client. Let me explain: if you have e.g. plain |
@giogonzo thank you for your answer!
yes I absolutely adore working with typescript in the full stack :) regarding your explanation: |
Anyway: this issue went on a bit too far, and you already gave many answers, so I think it's fair to close it. Thank for your help guys! |
I have a few questions on issues I am encountering when upgrading from 1.3 to 1.8.
I already created some separate issues that turned out not to be real issues but my lack of experience with the library, so in order to bother @gcanti the least possible I'll rather write all my questions here, so that we can hopefully avoid polluting the issuetracker.
I apologize if it's not the best approach.
I am using typescript 3.4.0-dev.20190312
t.type({foo: t.union([t.string, t.readonlyArray(t.string)])})
gives me the error:
Argument of type '[StringC, ReadonlyArrayC<StringC>]' is not assignable to parameter of type '[Mixed, Mixed, Mixed[] | undefined]'.
Same thing when I try a union with t.null for a nullable type.
2)I want to extend the type coming from a t.intersection of other 2 types. Is the correct way to do something like:
t.type({...intersectionType.types[0].props, ...intersectionType.types[1].props, extraField: t.string})
?
3)Does t.partial allow extra non-specified fields similarly to t.type? Do I have to use t.exact(t.partial({})) if I want to strip the extra fields?
In case of a type with optional parameters, does it translate to:
?
Is this the correct way to define such a type or are there better ways?
this seems to give me the following error:
Thanks for any help
The text was updated successfully, but these errors were encountered: