Subscribe on changes!

Let the type of sources in watch always be a tupel

avatar
Nov 21st 2020

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
avatar
Nov 22nd 2020

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

avatar
Nov 22nd 2020

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.

avatar
Dec 4th 2020

closing this because of #2425