typescript - 如何导出函数类型?

标签 typescript

这就是我为对象导出和导入 typescript 接口(interface)的方式。一切正常。以此为例,说明我要实现的目标,但具有功能。

模块 1

export interface MyInterface {
  property: string;
}

模块 2

import {MyInterface} from './module1';

const object: MyInterface = {
    property: 'some value'
};

下面的代码给我一个错误“TS2304:找不到名称‘myFunction’”。如何导出和导入函数类型?

模块 1

export let myFunction: (argument: string) => void;

模块 2

import {myFunction} from './module1';

let someFunction: myFunction; 

最佳答案

是这样的:

模块 1

export type myFunction = (arg: string) => void

模块 2

import {myFunction} from './module1';

let someFunction: myFunction; 

关于typescript - 如何导出函数类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43931361/

相关文章:

reactjs - 如何使用Electron和React在页面之间导航?

javascript - 如何动态更改下拉选项

Typescript 导入语句

javascript - TypeScript -- 来自全局范围的 new Image()

typescript - 如何声明两个不可赋值的类型?

类实现接口(interface)时 typescript 不正确的类型推断

javascript - 如何在 Angular 2+ 中获取 json 数据

reactjs - 要求至少将一个 prop 传递给组件 [typescript]

ReactJS Material UI withStyles incl。 TS 中的主题

Typescript - 如何访问通过命名空间声明的类?