简体   繁体   English

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

[英]How to Properly Export and Import Modules in TypeScript

Note: I know there are many posts on this topic, and I've reviewed quite a few already without success (please see my references at the bottom of this post). 注意: 我知道有很多关于该主题的文章,并且我已经回顾了很多没有成功的文章(请参阅本文底部的参考)。

I'm trying to run a very simple test in TypeScript, using Visual Studio Code, where I declare a class in one file and import it into another file. 我试图使用Visual Studio Code在TypeScript中运行一个非常简单的测试,在其中我在一个文件中声明一个类并将其导入到另一个文件中。 However, I continue to run into an issue where the file I am importing into is not recognizing the methods of the class that I exported from the other file. 但是,我仍然遇到一个问题,即我要导入的文件无法识别从其他文件导出的类的方法。

The exact error messages that I'm receiving at this point are: 我现在收到的确切错误消息是:

[ts] Property 'getFirstName' does not exist on type 'typeof "module-test/src/OtherClass"'. [ts]属性'getFirstName'在类型'typeof“ module-test / src / OtherClass”'上不存在。

[ts] Property 'getLastName' does not exist on type 'typeof "module-test/src/OtherClass"'. [ts]属性'getLastName'在类型'typeof“ module-test / src / OtherClass”'上不存在。

I'm using Node 9.3.0 and TypeScript 2.6.2. 我正在使用Node 9.3.0和TypeScript 2.6.2。

Many thanks in advance for any guidance that anyone can offer me! 在此先非常感谢任何人都可以提供给我的指导!

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(); 

OtherClass.ts OtherClass.ts

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

Things I've Tried 我尝试过的事情

  1. Exporting via the declaration 通过声明导出

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

  1. Exporting the individual functions 导出单个功能

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

  1. Multiple export statements 多个出口报表

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

  1. Multiple import statements 多个导入语句

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

Configuration Files package.json 配置文件 package.json

 { "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" } } 

tsconfig.json tsconfig.json

 { "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. */ } } 

Articles Referenced 参考文章

Where to begin? 从哪里开始? This is many edits away from a valid program; 远离有效程序的许多编辑; you should probably start with a working example as it's not 100% clear what you want this code to do. 您可能应该从一个有效的示例开始,因为并不能1​​00%清楚您此代码执行的操作。

You created a module with a single named export, the class Other . 您使用一个名为export的类Other创建一个模块。

export { Other };

You then imported the surrounding module object : 然后,您导入了周围的模块对象

import * as Other from "./OtherClass";

In the importing file, the class now has the name Other.Other . 现在,在导入文件中,该类的名称为Other.Other But at no point in the code did you actually instantiate the class with new ! 但是实际上,您在代码中都没有使用new 实例化该类! So here 所以在这里

let firstName = await Other.getFirstName();

You need to write 你需要写

const oth = new Other.Other();

This looks silly, of course. 当然,这看起来很愚蠢。 Change the import statement (don't have multiple!) to 更改导入语句(不必多!),以

import { Other } from "./OtherClass";

And now instead you can write 现在你可以写

const oth = new Other();

Moving on, we write 继续,我们写

let firstName = await oth.getFirstName();

Except that getFirstName isn't an async function, so you should really write 除了getFirstName不是异步函数之外,因此您应该真正编写

let firstName = oth.getFirstName();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM