-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore (react-hooks): improve type of selector argument in useShape ho…
…ok (#1610) The default value for the `selector` argument used to be typed as `never` which is wrong. This PR improves the type of the default value.
- Loading branch information
Showing
4 changed files
with
61 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@electric-sql/react": patch | ||
--- | ||
|
||
Improve type of selector argument in useShape hook. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { describe, expectTypeOf, it } from 'vitest' | ||
import { useShape, UseShapeResult } from '../src/react-hooks' | ||
|
||
describe(`useShape`, () => { | ||
it(`should infer correct return type when no selector is provided`, () => { | ||
const shape = useShape({ | ||
url: ``, | ||
}) | ||
|
||
expectTypeOf(shape).toEqualTypeOf<UseShapeResult>() | ||
}) | ||
|
||
type SelectorRetType = { | ||
foo: number | ||
bar: boolean | ||
baz: string | ||
} | ||
|
||
it(`should infer correct return type when a selector is provided`, () => { | ||
const shape = useShape({ | ||
url: ``, | ||
selector: (_value: UseShapeResult) => { | ||
return { | ||
foo: 5, | ||
bar: true, | ||
baz: `str`, | ||
} | ||
}, | ||
}) | ||
|
||
expectTypeOf(shape).toEqualTypeOf<SelectorRetType>() | ||
}) | ||
|
||
it(`should raise a type error if type argument does not equal inferred return type`, () => { | ||
const shape = useShape<number>({ | ||
url: ``, | ||
// @ts-expect-error - should have type mismatch, because doesn't match the declared `Number` type | ||
selector: (_value: UseShapeResult) => { | ||
return { | ||
foo: 5, | ||
bar: true, | ||
baz: `str`, | ||
} | ||
}, | ||
}) | ||
|
||
// Return type is based on the type argument | ||
expectTypeOf(shape).toEqualTypeOf<number>() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters