简体   繁体   English

检查泛型函数参数中的类型并返回此参数的对象字面量类型

[英]Check types in argument of generic function and return object literal type of this argument

My Task我的任务

I have two interfaces.我有两个接口。

interface ComponentOptions<Props> {
  abstract?: boolean;
  functional: boolean;
  props?: Props;
  name?: string;
  render?(props: Props): boolean;
}

interface ComponentProps {
  title: string;
  dark: boolean;
}

I need to realize a function that我需要实现一个功能

  • takes an interface to substitute the Props generic type需要一个接口来替代Props泛型类型
  • does any checks connected with the Props type in function argument object执行与函数参数对象中的Props类型相关的任何检查
  • returns an object literal type, based on the passed argument object根据传递的参数对象返回对象文字类型
    • does not return ComponentOptions<Props> interface不返回ComponentOptions<Props>接口

strict: true compiler option required. strict: true需要strict: true编译器选项。

Actual Solution实际解决方案

I have implemented createComponent function that recieves two arguments:我已经实现了接收两个参数的createComponent函数:

  • required options object literal必需的options对象文字
  • props , treated as imaginary argument for type casting props ,被视为类型转换的虚构参数
function createComponent<
  Props,
  Options extends ComponentOptions<Props>
>(options: Options, props: Props): Options {
  props;
  return options;
}

I achieved the desired result with such an realization.我通过这样的认识达到了预期的结果。 But I don't really like it.但我真的不喜欢它。

const component = createComponent({
  abstract: true,
  functional: true
}, {})

const componentWithProps = createComponent({
  functional: false,
  name: 'bar',
  props: {
    title: 'bar',
    dark: true
  },
  render(props) {
    return props.dark
  }
}, {} as ComponentProps)

The Problem问题

I want to get rid of props argument to left only options , and to set the type for props using a generic parameter, not casting the props argument.我想摆脱props参数只剩下options ,并使用通用参数设置 props 的类型,而不是强制转换props参数。

It might look like this:它可能看起来像这样:

const component = createComponent<ComponentProps>({
  functional: true,
  props: { // Check 'title' and 'dark' types
    title: 'comp',
    dark: true
  },
  render(props) { // Pass type for the props
    return props.dark;
  }
})

// When we hover on `component` variable,
// display all defined properties in the `options`
{
  functional: true,
  props: ComponentProps,
  render(props: ComponentProps): boolean
}

How to achieve this?如何实现这一目标?

Link to Playground 链接到游乐场

I solved my problem with this solution:我用这个解决方案解决了我的问题:

interface FunctionalComponent<Props> {
  <Options extends ComponentOptions<Props>>(opts: Options): Options
}

function createFunctionalComponent<Props = {}>() {
  return (o => o) as FunctionalComponent<Props>;
}

const component3 = createFunctionalComponent<ComponentProps>()({
  functional: false,
  name: 'bar',
  props: {
    title: 'bar',
    dark: true
  },
  render(props) {
    return props.dark
  }
});

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

相关问题 基于字符串文字类型参数的变量返回类型 - Variable return types based on string literal type argument 带有返回的通用 function 参数的 TypeScript 参数类型推断? - TypeScript argument type inference for a generic function argument with return? 返回参数类型为传入 function 参数值的函数的 object - Return object of functions with argument types of incoming function argument values 组合通用 function 参数类型 - Combining generic function argument types 依赖泛型参数作为 function 的值参数的类型 - Type that depends on generic argument as value argument to function 函数中的泛型不检查Typescript中的参数类型 - Generic in function doesn't check argument type in Typescript 通用部分应用程序的 Varargs function 参数不进行类型检查 - Varargs function argument of generic partial application does not type check 是否可以从传递给函数的参数中返回泛型类型 - Is it possible to return generic type from argument passed to a function 如何根据包含泛型的参数推断函数的返回类型? - How to infer the return type of a function based on an argument that contains a generic? 通过 typescript 中函数的可选参数返回不同的泛型类型 - return different generic type via function's optional argument in typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM