简体   繁体   English

如何从 class 属性(将 class 作为参数传递后)获取计算属性名称的文字类型?

[英]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: TS 将数据识别为以下类型:

const data: {} | undefined

I'd like TS to know that data has a property books我想让 TS 知道 data 有一个属性 books

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

Is it possible?可能吗?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 function 参数中获取计算属性名称的文字类型? - How to get a literal type from a function argument for a computed property name? 如何键入计算的属性名称? - How to type a computed property name? 类型文字中的计算属性名称必须引用类型为文字类型或“唯一符号”类型的表达式 - A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type 如何在TypeScript中获取属性名称和类名称 - How to get property name and class name in TypeScript 如何使用&#39;name&#39;属性为类指定类型? - How can a type be specified for a class with the 'name' property? 打字稿:如何仅从类类型获取所有属性装饰器? - Typescript: How to get all property decorators from only the class type? 接口中的计算属性名称必须引用其类型为文字类型或“唯一符号”类型的表达式 - A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type 打字稿:类型文字中的计算属性名称必须引用类型为文字类型或“唯一符号”的表达式 type.ts(1170) - Typescript : A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type.ts(1170) Angular 按名称获取类的属性 - Angular get property of class by name TypeScript 类型文字中的计算属性名称必须直接引用内置符号 - TypeScript A computed property name in a type literal must directly refer to a built-in symbol
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM