DocumentReference<T>のTの型の違いを認識させるには
解決したいこと
以下のようなモデルを作成します
interface Group {
a: DocumentReference<A>;
b: DocumentReference<B>;
}
当然期待する動作としてそれぞれの設定値についてA,Bの型以外はエラーではじいてほしいのですがされず困っています。
発生している問題・エラー
import {
getFirestore,
setDoc,
doc,
collection,
FirestoreDataConverter,
WithFieldValue,
QueryDocumentSnapshot,
DocumentReference,
} from "firebase/firestore";
const firestore = getFirestore();
interface A {
type: "A";
}
interface B {
type: "B";
}
interface Group {
a: DocumentReference<A>;
b: DocumentReference<B>;
}
(async () => {
{
const gRef: DocumentReference<Group> = doc(
collection(firestore, "groups").withConverter(converter<Group>()),
"group"
);
const aRef: DocumentReference<A> = doc(
collection(firestore, "a").withConverter(converter<A>()),
"a"
);
const bRef: DocumentReference<B> = doc(
collection(firestore, "enemies").withConverter(converter<B>()),
"b"
);
await setDoc(gRef, {
a: aRef,
b: aRef, // <-- ここがエラーになることを期待していますがエラーになってくれません、、、
});
}
})();
const converter = <T>(): FirestoreDataConverter<T> => ({
toFirestore: (data: WithFieldValue<T>) => {
return data;
},
fromFirestore: (snapshot: QueryDocumentSnapshot<T>) => {
return { ...snapshot.data() };
},
});
上記のこの箇所です
bはDocumentReference<B>
のみしか許可しない想定でしたがDocumentReference<A>
も受け入れてしまいます
// gRef: DocumentReference<Group>
await setDoc(gRef, {
a: aRef,
b: aRef, // <-- ここがエラーになることを期待していますがエラーになってくれません、、、
});
0