Skip to content

Type-unsafe behavior of watch #759

Closed
@clemyan

Description

@clemyan

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>,
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions