-
Notifications
You must be signed in to change notification settings - Fork 123
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
Can the .show() method be smarter about its return type? #105
Comments
You can using NiceModal.show<boolean>(someModal).then((res) => {
// res is boolean you passed in
console.log(res);
}); |
能在 someModal 里面定义,然后 show 的时候自动推断吗? |
Any chance to have types support also for imperative code (using hooks)? const modal = useModal(MyModal);
...
modal.show<ReturnType>().then(result => {}) |
This would be very nice! I think it should at least be a return type specified in the ModalComponent which is checked against my resolve values and then used by the .show() function. Something like this: const ConfirmDialog = NiceModal.create<
{
//input
title: string
},
//output
boolean
>(({ title }) => {
const modal = useModal()
...
// would error if resolve value is not typeof boolean
modal.resolve(true)
...
}) // confirmResult is typeof boolean (specified from the output of ConfirmDialog)
const confirmResult = await NiceModal.show(
ConfirmDialog,
{ title: 'Hi' }
) |
I think you can pass a modal parameter with a return type through const ConfirmDialog = NiceModal.create<{
//input
title: string
},
//output
boolean
>((modal, { title }) => {
...
// would error if resolve value is not typeof boolean
modal.resolve(true)
...
}) |
That won't work. Create only has a single generic:
|
nice-modal-react/src/index.tsx
Line 216 in 979c283
Right now, the
.show()
method always returnsPromise<unknown>
. If I'm not mistaken, whatever I end up passing into.resolve()
is what gets returned to.show()
.I'm only still learning TypeScript, but can't you use generics to accept a type? Right now, I'm having to do a manual type-check method in order to assert my return type. It feels like I shouldn't have to do that.
For example, if when constructing the modal, you do
In the app when you do
TypeScript thinks
res
isunknown
. Surely there's a way I can assert that it's a boolean?The text was updated successfully, but these errors were encountered: