Skip to content

Instantly share code, notes, and snippets.

@tonkotsuboy
Created February 27, 2020 02:34
Show Gist options
  • Select an option

  • Save tonkotsuboy/34ccddab14a81be2b49dfee74078fad7 to your computer and use it in GitHub Desktop.

Select an option

Save tonkotsuboy/34ccddab14a81be2b49dfee74078fad7 to your computer and use it in GitHub Desktop.
Unwrap<HTMLDivElement>の型の説明.md

divRef.value の戻り値の方が Unwrap<HTMLDivElement> になっている。 https://github.com/vuejs/vue-next/blob/e67f655b2687042fcc74dc0993581405abed56de/packages/reactivity/src/ref.ts#L106-L118 を見ると、Unwrap<T>は、次のような型定義

  1. cRef, ref, array, objectというキーを持っている
  2. []でキー名にアクセスしているが、その中でConditional Types(型定義では頻出。めちゃ便利)を使っている
  3. ref<HTMLDivElement>()の戻り値の方がUnwrap<HTMLDivElement>だから、 T extends object ? 'object' : 'ref' よりキー名がobject になる
  4. キー名が object なら { [K in keyof T]: UnwrapRef<T[K]> } が返るから、HTMLDivElement型にはならない。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment