-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
fix(types): improve type inference and correct some return types #1983
Conversation
|
||
/** | ||
* Normalizes the arguments you can give to Swal.fire() in an object of type SweetAlertOptions. | ||
* | ||
* Example: | ||
* ``` | ||
* Swal.argsToParams(['title', 'text']); //=> { title: 'title', text: 'text' } | ||
* Swal.argsToParams({ title: 'title', text: 'text' }); //=> { title: 'title', text: 'text' } | ||
* Swal.argsToParams([{ title: 'title', text: 'text' }]); //=> { title: 'title', text: 'text' } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it was wrong. Can you confirm it, @limonte?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirming 👍
sweetalert2.d.ts
Outdated
type SyncOrAsync<T> = T | Promise<T>; | ||
|
||
type ValueOrThunk<T> = T | (() => T); | ||
|
||
type UpdatableParameters = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better to export this to perhaps use it in other packages like ngx-sweetalert2?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, please do export UpdatableParameters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing contribution @rafaelss95!
Please export UpdatableParameters
and I'll merge this PR.
|
||
/** | ||
* Normalizes the arguments you can give to Swal.fire() in an object of type SweetAlertOptions. | ||
* | ||
* Example: | ||
* ``` | ||
* Swal.argsToParams(['title', 'text']); //=> { title: 'title', text: 'text' } | ||
* Swal.argsToParams({ title: 'title', text: 'text' }); //=> { title: 'title', text: 'text' } | ||
* Swal.argsToParams([{ title: 'title', text: 'text' }]); //=> { title: 'title', text: 'text' } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirming 👍
sweetalert2.d.ts
Outdated
type SyncOrAsync<T> = T | Promise<T>; | ||
|
||
type ValueOrThunk<T> = T | (() => T); | ||
|
||
type UpdatableParameters = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, please do export UpdatableParameters
In the last commit, I've updated some |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great contribution, thank you so much @rafaelss95!
## [9.13.3](v9.13.2...v9.13.3) (2020-05-29) ### Bug Fixes * **types:** improve type inference and correct some return types ([#1983](#1983)) ([d2e47cd](d2e47cd))
🎉 This PR is included in version 9.13.3 🎉 The release is available on: Your semantic-release bot 📦🚀 |
@limonte Pff, I just noticed that I forgot to add the |
No worries at all! just send another PR with the fix and I'll release the fix! |
I believe 0 users started to use |
Haha, done. #1984. |
Hey @rafaelss95 In our Angular integration this change seem to cause the issue: https://github.com/sweetalert2/ngx-sweetalert2/runs/747983015
Should we revert |
Hello, I guess we can simply solve this without mutations, using
|
Hello, I did not step in when you posted (cool PR btw!) but I think I should have ; my only concern was the addition of the readonly modifier. Technically, those properties aren't really readonly. For example, you may totally do : const options: SweetAlertOptions = { title: '...' };
if (something) {
options.onClose = () => { ... }
} In fact, that's what I do in ngx-sweetalert2. It's just legit. While I get that this provides some semantic information about how the options should be maipulated in general (and that's the good part and that's why I didn't intervene in the first place), it is also a constraint that's technically not really justified and now it caused problems. I don't have a very strong opinion on this though. What do you think ? :) On another subject : some of the improvements on the types here were not made before because of backwards compatibility. For example, generic types with default values (in SweetAlertOptions) were not possible before. However, I think that now may be a good time, it has been implemented in TypeScript for a while now (along with Pick for update() I think). But I wanted to warn about this since it hadn't been mentioned yet. You two have a great day! |
I can see that the If possible, I'd like to ask you @rafaelss95 to rollback the |
Hello @toverux... I could argue that if the options are directly mutated it can hide bugs in first place, because if someone try to update a non updatable parameter, like below, it'll fail silently at static analysis. I know there's a warning in update's function, but in really we still lose that ability we gain from TS: options.target = '#someId';
Well, since there's the if (something) {
swal.update({
onClose = () => { ... }
})
} Btw, what's the file/line that you do this there?
@limonte Hmm, I didn't go deep into this code, but shouldn't I mean... before it was:
No problem :). That all being said, I have no problem in create a PR rollbacking this. I'll do this ASAP. I just wanted to point that IMHO the |
if (something) {
swal.update({
onClose = () => { ... }
})
} It's not the same use-case. You can legitimately mutate the options object before showing the swal. That's what I meant by saying "that's what I do in ngx-sweetalert2": I don't have this particular code, instead I was talking about my foreach function to mutate the object before showing the swal. I know we can use reduce instead, and I prefer that too, but some may prefer the imperative style, or can't use reduce, like in my example. Instead, with readonly modifiers, they'd have do to something like this: const options: SweetAlertOptions = {
...someCondition ? { onClose() { /* ... */ } } : {}
}; I am aware that these are rare use cases though. |
Hmm, when I read it yesterday I thought you're talking about a open swal. Sorry for the confusion. |
…etalert2#1983) * fix(types): improve type inference and correct some return types * fixup! fix(types): improve type inference and correct some return types * fixup! fix(types): improve type inference and correct some return types
## [9.13.3](sweetalert2/sweetalert2@v9.13.2...v9.13.3) (2020-05-29) ### Bug Fixes * **types:** improve type inference and correct some return types ([sweetalert2#1983](sweetalert2#1983)) ([60f4bbf](sweetalert2@60f4bbf))
ReadonlyMap
isn't supported ininputOptions
. This PR offers support for both versions:Map
andReadonlyMap
;null
at some point and so it should be correctly represented with the type:HTMLElement | null
;preConfirm
result and another forpreConfirm
callback value, e.g.:ReadonlyArray
.UpdatableParameters
described insweetalert2/src/utils/params.js
Line 80 in 91700e7
SweetAlertOptions
are now readonly to better represent that even if you set something manually, it has no effect, like: