E
E
Eugene2020-09-07 14:03:50
typescript
Eugene, 2020-09-07 14:03:50

How to re-export a Typescript React component?

I have a component located in the src/MyComponent folder:
src/MyComponent/MyComponent.tsx - file with the component
src/MyComponent/index.js - the file imports and exports the component. It is needed in order to have access to the component via a short link (src/MyComponent instead of src/MyComponent/MyComponent)

When I import a component via a long link src/MyComponent/MyComponent, in the editor I see all the properties of the component in a tooltip. When I import from a short link, the properties disappear. Tell me how can I export the component so that both the component and its interface can be accessed by a short link?

The only option I found is to create an index.d.ts file:

import * as React from 'react';
Import MyComponentProps from './types.d';

declare const MyComponent: React.FC<MyComponentProps>;

export default MyComponent;


But here we have to re-describe the type of the same component. Is it possible to do it in a more beautiful way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2020-09-07
@eugeneledenev

I figured it out :
index.d.ts: It is important to specify the component in the From field, and not the .d.ts file
export { default } from './MyComponent';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question