Search code examples
typescripttypescript-typingsapollo-clienttypescript-genericsclass-transformer

How to get a literal type from a class property (after passing the class as argument) for a computed property name?


a moment ago I asked this question and now I have a follow up one :)

Please consider the following code:

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 recognizes data as the following type:

const data: {} | undefined

I'd like TS to know that data has a property books

const data: {
    books: Book[];
} | undefined

Is it possible?


Solution

  • This is possible with a combination of index access types and mapped types (playground):

    class Book {
      readonly many = "books" as const;
      bookData: any;
    }
    
    class Page {
      readonly many = "pages" as const;
      bookData: any;
    }
    
    type ManyType = { readonly many: string };
    
    type QueryResult<T extends ManyType> = {
      // using K in T["many"] to create an object with a key of value T["many"], e.g. "books", "pages", etc.
      [K in T["many"]]: T[]; 
    };
    
    type ClassConstructor<T> = new (...args: any[]) => T;
    
    function useQueryWrapper<T extends ManyType>(
      cls: ClassConstructor<T>,
      queryString: string
    ): QueryResult<T> | undefined {
      return {} as any;
    }
    
    const books = useQueryWrapper(Book, "...")?.books; // Book[] | undefined
    const pages = useQueryWrapper(Page, "...")?.pages; // Page[] | undefined