Skip to content
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

Type-unsafe behavior of watch #759

Closed
clemyan opened this issue Feb 21, 2020 · 4 comments
Closed

Type-unsafe behavior of watch #759

clemyan opened this issue Feb 21, 2020 · 4 comments

Comments

@clemyan
Copy link

clemyan commented Feb 21, 2020

Version

3.0.0-alpha.5

Reproduction link

http://www.typescriptlang.org/v2/en/play?ts=3.7.5#code/JYWwDg9gTgLgBAbzlApgMwDRwO4EMYDGAFlgErpwC+caUEIcA5AAIBuArigPRTsB2MUCgC0BaCkYAoSWL4BneGjgBeOAAooALjjk0AHj7sQAIxRQAfHAA+cQybMBKFZYSS4cWQo8QzBFABMVZHQNBzccfGI1MV8ArDVWJ2UXcPcuLjgAUQAPFDAUAhhtVjhgOVsjUyhUuHS4AEFC9lwAG2LS8t0DSrNLGzsqms8IFpQAOhaIAHMEuABqOABGMPdKMMpJIA

Steps to reproduce

  1. The typescript playground link shows error

What is expected?

v is of type number

What is actually happening?

v is of type Ref<number> | number


This overload of watch is typed as (simplified)

watch<T>(
  source: WatchSource<T>,
  cb: WatchCallback<T, T>
)

which is type-unsafe because if source is Ref<T>, source.value is UnwrapRef<T>, which cannot be passed to WatchCallback<T, T>.

TypeScript fails to catch the error since the underlying call doWatch is typed using any:

doWatch(
  source: WatchSource<any>,
  cb: WatchCallback<any, any>,
)
@yyx990803
Copy link
Member

Seems to be a problem in ref's typing instead of watch, since the type of coerced should be Ref<number> instead of Ref<number | Ref<number>>.

@clemyan
Copy link
Author

clemyan commented Feb 21, 2020

So you are saying ref should be typed as <T>(v: T) => Ref<UnwrapRef<T>>?

Oops, didn't see a commit closed this issue.

@clemyan
Copy link
Author

clemyan commented Feb 24, 2020

I see this specific instance of the issue is fixed in alpha.6. However, I still think this is a problem with watch. As long as the value property of Ref<T> is typed as UnwrapRef<T>, it is not type-safe to treat ref.value as T and pass it to WatchCallback<T, T>.

Type-unsafe example

@yyx990803
Copy link
Member

That's a good catch. I think ref value unwrapping should happen in its creation signature - see d4c6957

@github-actions github-actions bot locked and limited conversation to collaborators Nov 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants