提问者:小点点

如何在TypeScript中正确导出和导入模块


注意:我知道有很多关于这个主题的帖子,我已经审阅了相当多的没有成功(请看我在这篇文章底部的参考资料)。

我正试图使用Visual Studio代码在TypeScript中运行一个非常简单的测试,其中我在一个文件中声明一个类并将其导入到另一个文件中。但是,我仍然遇到一个问题,我正在导入的文件无法识别我从另一个文件导出的类的方法。

此时我收到的确切错误消息是:

[ts]属性“Get FirstName”在类型“typeof”module-test/src/otherClass“”上不存在。

[ts]属性“Get LastName”在类型“typeof”module-test/src/otherClass“”上不存在。

我使用的是Node 9.3.0和TypeScript 2.6.2。

非常感谢任何人为我提供的任何指导!

Main.TS

import * as Other from "./OtherClass";

class myApp
{
  public async start()
  {
    console.log("Starting application");
    
    try
    {
      let firstName = await Other.getFirstName();
      console.log("First Name: " + firstName);
      let lastName = await Other.getLastName();
      console.log("Last Name: " + lastName);
    }
    catch (error)
    {
      console.error("Unable to get name: " + error)
    }
    
    console.log("Ending application");
  }
}

const app = new myApp();
app.start();

null

OtherClass.ts

null

class Other
{
  getFirstName(): string
  {
    return "John";
  }

  getLastName(): string
  {
    return "Doe";
  }
}

export { Other };

null

我尝试过的事情

  1. 通过声明导出

null

export class Other
{
  getFirstName(): string
  {
    return "John";
  }

  getLastName(): string
  {
    return "Doe";
  }
}

null

null

class Other
{
  export function getFirstName(): string
  {
    return "John";
  }

  export function getLastName(): string
  {
    return "Doe";
  }
}

null

null

module.exports = Other;
export { Other };
export * from "OtherClass";

null

null

import * as Other from "./OtherClass";
import { Other } from "./OtherClass";

null

配置文件package.json

null

{
  "name": "module-test",
  "version": "1.0.0",
  "description": "Simple test of exporting and importing modules",
  "main": "./lib/main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "John Doe",
  "license": "ISC",
  "dependencies": {
    "typescript": "^2.6.2"
  },
  "devDependencies": {
    "@types/node": "^8.5.2",
    "@types/typescript": "^2.0.0"
  }
}

null

tsconfig.json

null

{
  "compilerOptions": {
    /* Basic Options */
    "target": "es2016",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    "outDir": "./lib/",                        /* Redirect output structure to the directory. */
    "strict": true,                            /* Enable all strict type-checking options. */
    "inlineSourceMap": true,               /* Emit a single file with source maps instead of having a separate file. */
    "inlineSources": true                 /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
  }
}

null

引用的文章

  • 属性“server”在类型“typeof”http“”
  • 上不存在
  • node.js ES6如何从模块导出类?
  • CommonJs模块系统中“Module.exports”和“exports”之间的差异
  • 如何在TypeScript中正确创建和导入节点模块
  • TypeScript-导出和导入模块
  • https://www.typescriptlang.org/docs/handbook/modules.html

共1个答案

匿名用户

从哪里开始?这是许多编辑远离一个有效的程序;您可能应该从一个工作示例开始,因为您想要这段代码做什么还不是100%清楚。

您创建了一个具有单个命名导出的模块,即类other

export { Other };

然后导入周围的模块对象:

import * as Other from "./OtherClass";

在导入文件中,类现在具有名称other.other。但是在代码中,您并没有实际使用new实例化类!所以在这里

let firstName = await Other.getFirstName();

你需要写

const oth = new Other.Other();

当然,这看起来很傻。更改import语句(不要有多个!)至

import { Other } from "./OtherClass";

现在你可以写了

const oth = new Other();

继续往前走,我们写

let firstName = await oth.getFirstName();

只不过getfirstname不是一个异步函数,所以您应该真正编写

let firstName = oth.getFirstName();