-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
Correct Hapi route cache configuration typing #16416
Conversation
…ptional) and expiresIn/At as mutually exclusive
types/hapi/index.d.ts to author (@jasonswearingen). Could you review this PR? Checklist
|
👍 looks good! |
@theefer I saw your typings and got excited again by the prospect of enforcing what you're hoping to enforce there. But as you can see from the tests it doesn't do what you're hoping for unfortunately. For example you can still define both: type OneOrTheOther = ({
expiresIn: number;
} | {
expiresAt: string;
});
const unsupportedOnlyOne: OneOrTheOther = {
expiresIn: 5000,
// expecting an error about having both attributes defined but no error as
// this object correctly fulfils the type requirements of `OneOrTheOther`.
// I suspect it might have something to do with Liskov Substitution Principle
// but not sure http://stackoverflow.com/a/23305134/539490
expiresAt: '22:44',
} In fact the compiler is happy with one with a wrong type as long as the other is present and correct: const badOnlyOne: OneOrTheOther = {
expiresIn: '5000', // expecting error but none
expiresAt: '22:44',
} In fact with the
|
Exclusive unions would be useful for this: microsoft/TypeScript#14094 |
Right, we need this as it works as expected:
(Obviously now the typings still have minimal gain for quite a lot of complexity but at least it works as we'd like it to) |
Thanks for correcting my mistakes, sorry it was my first attempt at an official d.ts contribution and I'm still learning and figuring out how to test these things properly. I saw that you included for fixed version in your branch, so shall I just close this PR? |
There's absolutely no need for apology. You saw (part of) the learning trip I took to get to the current solution... I should be thanking you... so thank you! :) The version of v15/index.d.ts in my commit is what your index.d.ts is at the moment. I'm happy for you to close this PR and I'll update the v15/index.d.ts in mine. Or you can update yours and have this PR merged in. Whatever you wish. If you want these changes asap I would recommend you update your index.d.ts and then we'll ping a maintainer to merge this PR as #16065 is taking a while. |
Ahh, I too was excited... perhaps too excided. I thought that type-union enforcement was a new feature of 2.3, but I guess not :(. Thanks for the actual in-ide check @AJamesPhillips |
merged #16065 |
@theefer feel free to update the v15 typings with these changes if you wish and submit another PR 👍 Thank you very much for this though. The changes are in the reworked hapi.js 16.1 typings thanks to you :) |
No worries, thanks for the update! |
Update types to include all options (as optional) and expiresIn/At as mutually exclusive
npm run lint package-name
(ortsc
if notslint.json
is present).If changing an existing definition:
Increase the version number in the header if appropriate.If you are making substantial changes, consider adding atslint.json
containing{ "extends": "dslint/dt.json" }
.