Let the type of sources in watch always be a tupel
What problem does this feature solve?
When watching multiple sources you don't have to do explicit const assertion on the input array.
What does the proposed API look like?
Since TypeScript 4.0 and the ability to use generics in the tupel spread syntax, the sources
parameter in watch
can always be made a tupel type. Basically change the type of sources
from T
to readonly [...T]
:
function watch<
T extends Readonly<Array<WatchSource<unknown> | object>>,
Immediate extends Readonly<boolean> = false
>(
sources: T,
cb: WatchCallback<MapSources<T, false>, MapSources<T, Immediate>>,
options?: WatchOptions<Immediate>
): WatchStopHandle
// ----->
function watch<
T extends Readonly<Array<WatchSource<unknown> | object>>,
Immediate extends Readonly<boolean> = false
>(
sources: readonly [...T],
cb: WatchCallback<MapSources<T, false>, MapSources<T, Immediate>>,
options?: WatchOptions<Immediate>
): WatchStopHandle
Wouldn't this be a breaking change for users with TypeScript < 4? It would be perfect if it could be introduced without breaking usage for TS 3.x
Yes, this would not work in a version smaller 4.0. In previous versions, there is nothing similar that I know of at least. However, there are discussions like this one that make me believe, that there is not really a straightforward solution. I guess this can be kept in mind for the future.