当index.ts没有导出成员时,解析为index.d.ts

时间:2019-03-26 23:10:55

标签: typescript

说,我有以下文件:

  1. src / Button / index.tsx
  2. src / Button / index.d.ts
  3. src / App.tsx

文件如下。

src / Button / index.tsx:

import * as React from "react";
import {ButtonProps} from "./index.d"; // import from "." fails

export const Button: React.FunctionComponent<ButtonProps> = ({
  type,
  disabled,
  children
}: ButtonProps) => (
  <button type={type} disabled={disabled}>
    {children}
  </button>
);

src / Button / index.d.ts:

export type ButtonType = "button" | "reset" | "submit";

export interface ButtonProps {
  type: ButtonType;
  disabled: boolean;
  children: string;
}

src / App.tsx:

import * as React from "react";
import {render} from "react-dom";
import {Button} from "./Button";
import {ButtonType} from "./Button/index.d"; // import from "./Button" fails

const type: ButtonType = "reset";

const App = () => (
  <Button type={type} disabled>
    PRESS
  </Button>
);

render(<App />, document.getElementById("root"));

每当我尝试从定义文件(xy.d.ts)导入类型或接口时,都无法使用导入中的文件夹快捷方式(请参阅代码中的注释),因为文件分辨率找到了可用的index.tsx。文件,但其中没有定义的导出。因此,是否有可能以某种方式在导入定义文件时使用目录快捷方式,而无需在index.tsx中导入和导出定义?或换种说法:如果给定的index.tsx没有具有给定名称的导出成员,是否可以将文件分辨率重定向到index.d.ts?

注意:每当我在Visual Studio代码中使用定义文件中的类型或接口时,它都会自动从目录中导入该类型/接口,因此编辑器似乎可以理解,但是当我将其捆绑时,捆绑器告诉我,他找不到出口。

0 个答案:

没有答案