How do I write the type definition for a module with a default export

╄→гoц情女王★ 提交于 2020-08-08 05:58:28

问题


I want to write a type definition for storybook-router. It needn't be that accurate, since this is a minor development tool (i.e. anys are acceptable), but I can't even seem to get that to work.

What I want to represent is:

import StoryRouter from 'storybook-router';

...in which StoryRouter is a function that matches the interface StoryDecorator from @types/storybook__react.

I tried the following definition file:

import { StoryDecorator } from '@storybook/react';

declare type StoryRouter = StoryDecorator;

export default StoryRouter;

Unfortuantely, that results in the following error:

TS7016: Could not find a declaration file for module 'storybook-router'. '/<path snipped>/node_modules/storybook-router/dist/StoryRouter.js' implicitly has an 'any' type. Try npm install @types/storybook-router if it exists or add a new declaration (.d.ts) file containing declare module 'storybook-router';

However, if I use that final example (declare module 'storybook-router'), I can't seem to figure out how to set a default export.

What's the correct way to represent this type?


回答1:


You don't import in a .d.ts file, you just declare the module.
Usually, it looks like this (simplest use-case with any):

// storybook-router.d.ts
declare module 'storybook-router' {
    const StoryDecorator:any;
    export default StoryDecorator;
}


来源:https://stackoverflow.com/questions/47903313/how-do-i-write-the-type-definition-for-a-module-with-a-default-export

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!