Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

コンポーネントのrefを追加する関数の型を修正 #1273

Merged
merged 1 commit into from
Apr 3, 2023

Conversation

k-chop
Copy link
Contributor

@k-chop k-chop commented Apr 1, 2023

内容

ref の型定義はこうなっています。

ref: VNodeRef

type VNodeRef = string | Ref | ((ref: Element | ComponentPublicInstance | null, refs: Record<string, any>) => void);

addAudioCellRef の型は (audioCellRef: typeof AudioCell) => void です。( typeof AudioCell ≒ ComponenPublicInstance
これが (ref: Element | ComponentPublicInstance | null, refs: Record<string, any>) => void と互換性がなくエラーになっていました

(A | B) => void を要求しているところに A => void は入れられない。 (A => void) | (B => void) ならOK)

addAudioCellRef の型は VNodeRef にしておき、中で型の絞り込みを行っています。
また、 AudioCell であることも分からないのでtype assertionしています。
<audio-cell> コンポーネントの ref を指定しているんだからvueが推論して欲しい気もしますね・・・

関連 Issue

@k-chop k-chop requested a review from a team as a code owner April 1, 2023 03:15
@k-chop k-chop requested review from y-chan and removed request for a team April 1, 2023 03:15
const addAudioCellRef = (audioCellRef: typeof AudioCell) => {
if (audioCellRef) {
audioCellRefs[audioCellRef.audioKey] = audioCellRef;
let audioCellRefs: Record<AudioKey, InstanceType<typeof AudioCell>> = {};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

子コンポーネントの参照を保持するときは InstanceType を使うべきだそうなので変更しています
https://ja.vuejs.org/guide/typescript/composition-api.html#typing-component-template-refs

Comment on lines +533 to 535
const addAccentPhraseElem: VNodeRef = (elem) => {
if (elem instanceof HTMLElement) {
accentPhraseElems.push(elem);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらも同じエラーが出ていたので修正しました
こっちはHTMLElementを期待しているのでちょっとシンプル

Copy link
Member

@y-chan y-chan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど...!

なんとなく問題と解決策について理解しました!LGTMです!

Copy link
Member

@Hiroshiba Hiroshiba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!!

ドキュメントのどこに記載されてるかわからなかったのですが、普通にTypeScriptのとこで探せばよかったんですね…😇

@Hiroshiba Hiroshiba merged commit 1c22078 into VOICEVOX:main Apr 3, 2023
@k-chop k-chop deleted the issue1271/fix-ref-type branch April 3, 2023 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

コンポーネントのrefをaddする部分の型エラーの直し方がわからない
3 participants