最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Generic React TypeScript component with 'as' prop (able to render any valid dom node) - Stack Overf

programmeradmin1浏览0评论

I have this working as expected in the below example, my question is - is there anyway i can rewrite this so that i don't have to pass both a generic T and the as prop. I would ideally like to just pass the as prop and have the ponent's prop interface use that.

Is this possible in TypeScript?

export type Props<
  T extends keyof JSX.IntrinsicElements
> = JSX.IntrinsicElements[T] & {
  as?: keyof JSX.IntrinsicElements
}

export const MyComponent = <T extends keyof JSX.IntrinsicElements>({
  as: Component = 'div',
}: Props<T>) => {
   // Stuff
   return <Component />
}


// Usage
const Anchor = () => <MyComponent<'a'> href='foo' as='a' id='bar' />

I have this working as expected in the below example, my question is - is there anyway i can rewrite this so that i don't have to pass both a generic T and the as prop. I would ideally like to just pass the as prop and have the ponent's prop interface use that.

Is this possible in TypeScript?

export type Props<
  T extends keyof JSX.IntrinsicElements
> = JSX.IntrinsicElements[T] & {
  as?: keyof JSX.IntrinsicElements
}

export const MyComponent = <T extends keyof JSX.IntrinsicElements>({
  as: Component = 'div',
}: Props<T>) => {
   // Stuff
   return <Component />
}


// Usage
const Anchor = () => <MyComponent<'a'> href='foo' as='a' id='bar' />
Share Improve this question asked Oct 2, 2019 at 11:44 SamuelSamuel 2,6356 gold badges34 silver badges41 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 10

It's fairly easy to implement the second variant — the one where an explicit type argument is required:

Solution one

import * as React from 'react';

type Props<K extends keyof JSX.IntrinsicElements> = JSX.IntrinsicElements[K];

declare class MyComponent<K extends keyof JSX.IntrinsicElements> extends React.Component<Props<K>> {}

<MyComponent<'a'> href="https://example./" id="myLink" />;

Solution two

When it es to the first variant, it's more tricky. What you want is not a generic ponent, but a union of props. To illustrate why, let's consider a concrete example when MyComponent handles only a union of a and button.

import * as React from 'react';

type Props =
   | ({ as: 'a' } & JSX.IntrinsicElements['a'])
   | ({ as: 'button' } & JSX.IntrinsicElements['button']);

declare class MyComponent<T extends 'a' | 'button'> extends React.Component<Props> {}

<MyComponent as="a" href="https://example." />; // ✔ OK
<MyComponent as="button" href="https://example." />; // ✘ Compile-time error

MyComponent doesn't have to be generic in order to recognize which props it should receive. The as prop is a sufficient discriminant.

We can generalize this example by creating a union of all tags and their respective props:

import * as React from 'react';

type Props = {
  [K in keyof JSX.IntrinsicElements]: { as: K } & JSX.IntrinsicElements[K];
}[keyof JSX.IntrinsicElements];

declare class MyComponent extends React.Component<Props> {}

<MyComponent as="a" href="https://example." />; // ✔ OK
<MyComponent as="button" href="https://example." />; // ✘ Compile-time error

This will get the job done, as it's the same as if we defined our union manually. However, creating such a huge union has downsides:

  • IntelliSense bees painfully slow
  • Error messages bee cryptic
  • The overall plexity increases.

Just something to be aware of! ;)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论