Skip to content

Commit 4475805

Browse files
committed
fix(useResult): data => data deep required & non-nullable, closes #1250
1 parent 3792daa commit 4475805

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

packages/vue-apollo-composable/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
},
3838
"dependencies": {
3939
"throttle-debounce": "^2.3.0",
40+
"ts-essentials": "^8.1.0",
4041
"vue-demi": "^0.9.1"
4142
},
4243
"peerDependencies": {

packages/vue-apollo-composable/src/useResult.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Ref, computed } from 'vue-demi'
22
import { ExtractSingleKey } from './util/ExtractSingleKey'
3+
import type { DeepNonNullable, DeepRequired } from 'ts-essentials'
34

45
export type UseResultReturn<T> = Readonly<Ref<Readonly<T>>>
56

@@ -60,7 +61,7 @@ export function useResult<
6061
> (
6162
result: Ref<TResult>,
6263
defaultValue: TDefaultValue | undefined,
63-
pick: (data: TResult) => TReturnValue
64+
pick: (data: DeepRequired<DeepNonNullable<TResult>>) => TReturnValue
6465
): UseResultReturn<TDefaultValue | TReturnValue>
6566

6667
export function useResult<
@@ -70,14 +71,14 @@ export function useResult<
7071
> (
7172
result: Ref<TResult>,
7273
defaultValue?: TDefaultValue,
73-
pick?: (data: TResult) => TReturnValue,
74+
pick?: (data: DeepRequired<DeepNonNullable<TResult>>) => TReturnValue,
7475
): UseResultReturn<TResult | TResult[keyof TResult] | TDefaultValue | TReturnValue | undefined> {
7576
return computed(() => {
7677
const value = result.value
7778
if (value) {
7879
if (pick) {
7980
try {
80-
return pick(value)
81+
return pick(value as DeepRequired<DeepNonNullable<TResult>>)
8182
} catch (e) {
8283
// Silent error
8384
}

packages/vue-apollo-composable/tests/types/useResult-types.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ const { result: multiKeyResult } = multiKeyQuery
107107
const useResult_WithPickFunction = useResult(
108108
multiKeyResult,
109109
[] as const,
110-
data => data?.otherExample?.__typename,
110+
data => data.otherExample.__typename,
111111
)
112112

113113
assertExactType<
114114
typeof useResult_WithPickFunction,
115-
UseResultReturn<'OtherExample' | [] | undefined>
115+
UseResultReturn<'OtherExample' | []>
116116
>(useResult_WithPickFunction)
117117

118118
if (typeof useResult_WithPickFunction.value === 'string') {

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -14918,6 +14918,11 @@ tryer@^1.0.1:
1491814918
resolved "https://registry.yarnpkg.com/tryer/-/tryer-1.0.1.tgz#f2c85406800b9b0f74c9f7465b81eaad241252f8"
1491914919
integrity sha512-c3zayb8/kWWpycWYg87P71E1S1ZL6b6IJxfb5fvsUgsf0S2MVGaDhDXXjDMpdCpfWXqptc+4mXwmiy1ypXqRAA==
1492014920

14921+
ts-essentials@^8.1.0:
14922+
version "8.1.0"
14923+
resolved "https://registry.yarnpkg.com/ts-essentials/-/ts-essentials-8.1.0.tgz#bc982b242db226b84c89477d3d42630ee2954513"
14924+
integrity sha512-34xALeQADWRYq9kbtprP4KdpTQ3v3BBIs/U4SpaP+C4++B8ijXY5NnILRJLchQVMzw7YBzKuGMUMrI9uT+ALVw==
14925+
1492114926
ts-invariant@^0.4.0:
1492214927
version "0.4.4"
1492314928
resolved "https://registry.yarnpkg.com/ts-invariant/-/ts-invariant-0.4.4.tgz#97a523518688f93aafad01b0e80eb803eb2abd86"

0 commit comments

Comments
 (0)