简体   繁体   English

打字稿通用函数,它根据其他变量提供组件属性

[英]Typescript generic function which gives component properties based on other variables

This one has been puzzling me for a while, trying to create a type-safe email service.这个问题让我困惑了一段时间,试图创建一个类型安全的电子邮件服务。

I have an enum of possible template names:我有一个可能的模板名称枚举:

enum TemplateName {
 EXAMPLE_TEMPLATE = "EXAMPLE TEMPLATE"
 ...
}

I have an object of default settings per template:我有每个模板的默认设置对象:

type EmailConfig<X = React.ComponentType> = {
 html: X
 subject: string
 ...
}

type EmailMapping: EmailConfig = {
 [key in TemplateName]: EmailConfig
}

const Emails = {
 [TemplateName.EXAMPLE_TEMPLATE]: {
    html: TestTemplate, // THIS IS A REACT FUNCTIONAL COMPONENT
    subject: "This is a test",
    ...rest
 }
 ...
}

My Templates look like so:我的模板看起来像这样:

export interface TestTemplateProps {
  title?: string
  firstName?: string
  preview?: string
  headline?: string
  site?: string
  ...
}

export const TestTemplate: React.FC<TestTemplateProps> = ({
  title = 'Test Email',
  site = 'My Website',
  preview = 'Important Information from My Site',
  firstName = 'there',
  headline,
  children,
}) => {
  return (
  ...
  )
}

I have a generic function I want to be able to pass in an enum value and all the props of the Component that relates to that enum value.我有一个通用函数,我希望能够传入一个枚举值以及与该枚举值相关的组件的所有道具。

FOR EXAMPLE例如

sendEmail(TemplateName.EXAMPLE_TEMPLATE, { ... })

Where { ... } is typed to TestTemplateProps interface其中 { ... } 被输入到 TestTemplateProps 接口

My current attempt at sendEmail looks like this:我目前对sendEmail尝试如下所示:

async sendEmail<X extends keyof EmailMapping>(
    template: X,
    opts: React.ComponentProps<typeof Emails[X]['html']>
  ) {
...
}

I've tried just playing around (honestly I am just guessing at what to change at this point) and this is the closest I have come so far.我试过只是玩玩(老实说,我只是在猜测此时要改变什么),这是迄今为止我最接近的一次。

When I call sendEmail with above code, it forced me to pass one of the Enums, but then in the opts the only "typing" that appears is "children?"当我用上面的代码调用 sendEmail 时,它迫使我通过枚举之一,但是在opts中唯一出现的“打字”是“孩子?” and none of the other properties in the TestTemplateProps so I think I'm close!并且 TestTemplateProps 中的其他属性都没有,所以我想我很接近了!

TIA TIA

Yes.是的。 but the first thing that I wanna know is ... are you making a class for data loading and passing data in your interfaces with static function or static constructor for async data loading... if so ... I want to know the the exact problem you're facing...但我想知道的第一件事是……您是否正在使用static functionstatic constructor static function在您的接口中创建一个用于数据加载和传递数据的类以进行异步数据加载……如果是这样……我想知道您面临的确切问题...

暂无
暂无

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

相关问题 Typescript + React:基于其他属性对象键的通用属性 - Typescript + React: Generic properties based on keys of other property object 具有函数重载和通用参数的 Typescript React 组件 - Typescript React component with function overload and generic params Typescript React:基于通用的回调事件处理程序 function 作为组件的道具 - Typescript React: generic-based callback event handler function as prop to component 如何将带有通用道具的React类组件转换为Typescript中的功能组件? - How to transform a React Class Component with generic props to Function Component in Typescript? TypeScript:处理既不引用 state 也不引用 props 的“this”组件属性 - TypeScript: dealing with `this` component properties which are reffering neither to state, nor to props 带有 TypeScript 的通用 React 组件 - Generic React Component with TypeScript TypeScript 抱怨 redux 注入的组件缺少属性 - TypeScript complaining about component missing properties which are injected by redux Typescript - 一个返回泛型函数的函数 - 它有什么作用? - Typescript - A function which returns a function with generic type - what does it do? 如何在 TypeScript 中正确键入通用 React function 组件 - How to correctly type a generic React function component in TypeScript TypeScript/React - 如何让 function 接受通用组件类型? - TypeScript/React - How to have function accept a generic component type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM