fix(useResult): data => data deep required & non-nullable, closes #1250

This commit is contained in:
Guillaume Chau
2021-09-20 17:56:33 +02:00
parent 3792daa586
commit 4475805cb6
4 changed files with 12 additions and 5 deletions
@@ -37,6 +37,7 @@
},
"dependencies": {
"throttle-debounce": "^2.3.0",
"ts-essentials": "^8.1.0",
"vue-demi": "^0.9.1"
},
"peerDependencies": {
@@ -1,5 +1,6 @@
import { Ref, computed } from 'vue-demi'
import { ExtractSingleKey } from './util/ExtractSingleKey'
import type { DeepNonNullable, DeepRequired } from 'ts-essentials'
export type UseResultReturn<T> = Readonly<Ref<Readonly<T>>>
@@ -60,7 +61,7 @@ export function useResult<
> (
result: Ref<TResult>,
defaultValue: TDefaultValue | undefined,
pick: (data: TResult) => TReturnValue
pick: (data: DeepRequired<DeepNonNullable<TResult>>) => TReturnValue
): UseResultReturn<TDefaultValue | TReturnValue>
export function useResult<
@@ -70,14 +71,14 @@ export function useResult<
> (
result: Ref<TResult>,
defaultValue?: TDefaultValue,
pick?: (data: TResult) => TReturnValue,
pick?: (data: DeepRequired<DeepNonNullable<TResult>>) => TReturnValue,
): UseResultReturn<TResult | TResult[keyof TResult] | TDefaultValue | TReturnValue | undefined> {
return computed(() => {
const value = result.value
if (value) {
if (pick) {
try {
return pick(value)
return pick(value as DeepRequired<DeepNonNullable<TResult>>)
} catch (e) {
// Silent error
}
@@ -107,12 +107,12 @@ const { result: multiKeyResult } = multiKeyQuery
const useResult_WithPickFunction = useResult(
multiKeyResult,
[] as const,
data => data?.otherExample?.__typename,
data => data.otherExample.__typename,
)
assertExactType<
typeof useResult_WithPickFunction,
UseResultReturn<'OtherExample' | [] | undefined>
UseResultReturn<'OtherExample' | []>
>(useResult_WithPickFunction)
if (typeof useResult_WithPickFunction.value === 'string') {
+5
View File
@@ -14918,6 +14918,11 @@ tryer@^1.0.1:
resolved "https://registry.yarnpkg.com/tryer/-/tryer-1.0.1.tgz#f2c85406800b9b0f74c9f7465b81eaad241252f8"
integrity sha512-c3zayb8/kWWpycWYg87P71E1S1ZL6b6IJxfb5fvsUgsf0S2MVGaDhDXXjDMpdCpfWXqptc+4mXwmiy1ypXqRAA==
ts-essentials@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/ts-essentials/-/ts-essentials-8.1.0.tgz#bc982b242db226b84c89477d3d42630ee2954513"
integrity sha512-34xALeQADWRYq9kbtprP4KdpTQ3v3BBIs/U4SpaP+C4++B8ijXY5NnILRJLchQVMzw7YBzKuGMUMrI9uT+ALVw==
ts-invariant@^0.4.0:
version "0.4.4"
resolved "https://registry.yarnpkg.com/ts-invariant/-/ts-invariant-0.4.4.tgz#97a523518688f93aafad01b0e80eb803eb2abd86"