刚才我问了这个问题,现在我有一个跟进:)
请考虑以下代码:
import { ClassConstructor } from "class-transformer";
import { useQuery as useApolloQuery } from "@apollo/client";
class Book {
readonly many = "books" as const;
bookData: any;
}
export const useQueryWrapper = <T>(cls: ClassConstructor<T>, queryString) => {
return useApolloQuery<{ [cls.prototype.many]: T[] }>(queryString);
};
const { data } = useQueryWrapper(Book, "..."); // Book or any other class with a literal `many` prop
TS 将数据识别为以下类型:
const data: {} | undefined
我想让 TS 知道 data 有一个属性 books
const data: {
books: Book[];
} | undefined
可能吗?