Closed thruflo closed 7 hours ago
@thruflo I explained usage of never
vs unknown
in this context here: https://github.com/electric-sql/electric/pull/1791#issuecomment-2396160005
We use never
as the default when we want to ensure that if no type argument is passed, we treat it as only the base types with no extensions. And we use unknown
whenever we want to allow the user to pass in any extended type.
If we look at your snippet:
export function matchStream<T extends Row>
The problem is that T
is said to extend Row
.
Since you don't pass any type argument to Row
it uses the default of never
, so in fact we have:
export function matchStream<T extends Row<never>>
Thus, we require T
to extend Row
and we don't allow any extensions since the extensions are set to never
.
But, matchStream(stream, ['delete'], matchBy('id', id))
is calling it with Todo
as a type argument which does define an extension. Hence, the type mismatch.
To fix this problem, you need to modify your definition of matchStream
to allow unknown extensions:
export function matchStream<T extends Row<unknown>>
Now, we're saying that T
can be a row type and may include extensions (whose type we actually don't know).
Brilliant, thanks for the lesson :)
Out of interest, I see GetExtensions used in the type signatures of a few of the react-hooks functions, like getShapeStream. Is there any scenario where I might use it directly?
Out of interest, I see GetExtensions used in the type signatures of a few of the react-hooks functions, like getShapeStream. Is there any scenario where I might use it directly?
You can use it whenever you need the type of the extensions but you only have the entire Row
type.
So, GetExtensions<Row<Todo>> = Todo
but you would use it when you need to get the extensions type from some type T
where T extends Row<unknown>
.
I have a type like this:
The
created_at
field is aDate
column, stored as atimestamptz
field in Postgres. This is not one of the types supported by the default parser, so I use a custom parser with myShapeStreamOptions
:I later call
stream.subscribe
with a callback function and get a type error. Just to give the full context, I callstream.subscribe
as part of thismatchStream
function:E.g. by calling:
This raises a type error:
Now, I see there's some clever type machinery in the
Row
type definition in the Typescript client insrc/types.ts
designed to support types using a custom parser:How do I use this when defining my
Todo
type to provide the information about the created_at field'sDate
type?