From 5359120ed3313a3b6bb5355e163c4e9a928ea562 Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Mon, 21 Sep 2020 18:38:33 +0800 Subject: [PATCH] types: improve useParent typing --- src/composition/use-relation.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/composition/use-relation.ts b/src/composition/use-relation.ts index 658deab55..a47778df6 100644 --- a/src/composition/use-relation.ts +++ b/src/composition/use-relation.ts @@ -1,11 +1,11 @@ import { inject, computed, onUnmounted } from 'vue'; -export type Parent = null | { - children: T[]; -}; +type Parent = { children: unknown[] }; -export function useParent(key: string, child: T = {} as T) { - const parent = inject>(key, null); +type Child = T extends { children: (infer U)[] } ? U : never; + +export function useParent

(key: string, child: Child

) { + const parent = inject

(key, null); if (parent) { const { children } = parent;