如何为具有默认导出的模块编写类型定义 [英] How do I write the type definition for a module with a default export

查看:63
本文介绍了如何为具有默认导出的模块编写类型定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 storybook-router 编写类型定义一>.它不需要那么准确,因为这是一个次要的开发工具(即 any 是可以接受的),但我似乎无法让它工作.

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.

我想代表的是:

import StoryRouter from 'storybook-router';

...其中 StoryRouter 是一个与 @types/storybook__react 中的接口 StoryDe​​corator 相匹配的函数.

...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:找不到模块storybook-router"的声明文件.'/<path snipped>/node_modules/storybook-router/dist/StoryRouter.js' 隐含地具有 'any' 类型.尝试 npm install @types/storybook-router 如果它存在或添加一个包含 declare module 'storybook-router';

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';

但是,如果我使用最后一个示例(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?

推荐答案

您无需在 .d.ts 文件中导入,您只需声明模块.
通常,它看起来像这样(最简单的用例):

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;
}

这篇关于如何为具有默认导出的模块编写类型定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆