Just a moment ago I posted this question on Stack Overflow, and now I have a follow-up query :)
Take a look at this code snippet:
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
TypeScript currently recognizes the 'data' variable as:
const data: {} | undefined
I want TypeScript to infer that 'data' should contain a property named 'books'
const data: {
books: Book[];
} | undefined
Is there a way to achieve this?