-
-
Notifications
You must be signed in to change notification settings - Fork 256
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: snapshot typing for ref #146
Conversation
@italodeandra Please try if this fixes your issue. |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit f288379:
|
I'm amazed on how I didn't have to do anything for testing your change. Codesandbox bot already grabbed my cb, updated it with the commit release, and forked with it. It's working flawlessly. Thanks! And I think it was nice that you liked my idea of the naming. Don't know how you feel about it but for me it feels nice. |
Off-topic question though: Why |
Yeah, codesandbox ci is nice. As for naming, I wasn't happy with
Because babel doesn't support const enum. |
@Aslemammad Do you want to review? |
@@ -15,7 +15,7 @@ import { | |||
|
|||
import { getVersion, subscribe, snapshot } from './vanilla' | |||
import { createMutableSource, useMutableSource } from './useMutableSource' | |||
import type { NonPromise } from './vanilla' | |||
import type { DeepResolveType } from './vanilla' |
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.
Actually, it's a great type name, I don't have any problem with it, but could it be shorter?
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.
DeepResolve
? ResolveType
?
DeepResolveType
feels better as it looks special.
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.
Yea, no problem with that, let's go with it.
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's a complex name for a complex utility type 😂
And by complex I mean special like @dai-shi said.
@dai-shi I use an external typing for the proxy. But the export type IMessage = {
id: string
content: string
nodeRef: Ref<HTMLDivElement> & ReturnType<typeof ref>
} Because this Here's the CB with the problem: https://codesandbox.io/s/valtio-ref-with-reactref-problem-2-25lxs?file=/src/App.tsx |
So, this doesn't work for you? (can't see it in the first screenshot.) export const drawerState = proxy({
currentPosition: 0,
isOpen: false,
isRendering: false,
ref: ref(createRef<HTMLDivElement>()),
startPosition: 0,
with: 0,
}) We don't want to export const dummyRef =
process.env.NODE_ENV === 'development'
? ref(createRef<HTMLButtonElement>())
: undefined
type MyRef = NonNullable<typeof dummyRef> |
Yes, you guessed the code right. I couldn't replicate in CB, so this could be related to yarn or WebStorm. Do you have any suggestion for me to help you fix it?
I agree with you. But in this case, I think we should aim to fix it without even needing to use this type, right? |
Yeah. Oh, I missed the error code TS4023. Or, if there were a better solution than const enum hack... |
I was able to repro the error with Let's collect more feedbacks/ideas and see what options we have. Meanwhile, if you need to create type files with export const state = proxy({
buttonRef: ref(createRef<HTMLButtonElement>()) as Ref<HTMLButtonElement>
});
export default function App() {
const { buttonRef } = useSnapshot(state);
useEffect(() => {
console.log(buttonRef);
}, [buttonRef]);
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
<button ref={buttonRef as Ref<HTMLButtonElement>}></button>
</div>
);
} |
But it feels weird to cast it like that.
I didn't fix the problem, I just switched to a different one. This solution #145 (comment) looked better to me. So I think this |
Solution with module augmentation sounds nice. Can you do it with the current version?
|
Yes, you're right. I didn't think about it. But it breaks type comparisons, because every property that used |
I edit this answer a couple times. It doesn't work with the export { ref, snapshot, getVersion, proxy, subscribe } from "valtio"
declare module "valtio" {
type Options = {
sync?: boolean
}
export declare type DeepResolveType<T> = T extends Function
? T
: T extends { current: unknown }
? T
: T extends Promise<infer V>
? V
: T extends object
? {
[K in keyof T]: DeepResolveType<T[K]>
}
: T
export declare const useSnapshot: <T extends object>(
proxyObject: T,
options?: Options | undefined
) => DeepResolveType<T>
} |
Oh, I didn't know you could augment const arrow function. I wonder if there's better way of |
No, my entire augmentation is only that. But to make it work on
I'll try to think of something. This enum hack is only typing, so I can make some tests on my own augmentation and see if anything breaks on the entire code. |
This is probably what I meant.
Yes. The point of |
Oh, right. I'm currently using |
fix #145
ref
type with const enumNonPromise
toDeepResolveType
(BREAKING CHANGE in types)