NodeJS如何将JS文件导入TypeScript [英] NodeJS How to import JS file into TypeScript

查看:789
本文介绍了NodeJS如何将JS文件导入TypeScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是TypeScript的新手.我目前正在学习使用Typescript语言的NodeJS Loopback 4 框架.我的问题是如何将某些功能(已在JS文件中导出的类)导入到我的TS文件中.经过几次搜索后,它仍然无法正常工作. 例如:

I'm new with TypeScript. I'm currently learning NodeJS Loopback 4 framework which use Typescript language. And my question is how to import some function, class which has been exported in JS file into my TS file. After search several way but it still doesn't work with me. Here's example:

  // /src/index.ts
    import {plus} from './lib/test';

    console.log(plus(1,2));


// /src/lib/test.js
    export function plus(x, y) {
      return x + y;
    }

我也尝试使用这样的定义打字稿

I also try using definition typescript like this

// /src/lib/test.d.ts
export declare function plus(x: number, y: number): number;

但是在index.ts文件中导入此函数时仍然出现错误

But still got error when import this function in index.ts file

错误:找不到模块'./lib/test' 在Function.Module._resolveFilename(module.js:543:15)

Error: Cannot find module './lib/test' at Function.Module._resolveFilename (module.js:543:15)

推荐答案

tsts.json似乎未启用'allowJs',因为它会导出声明.

It looks like the tsconfig.json doesn't have 'allowJs' enabled because it exports declarations.

您是否有理由不希望它成为打字稿文件?如果将test.js更改为test.ts,则将其设置为.ts应该可以在索引文件中识别它.

Is there a reason you are not wanting this to be a typescript file? If you change test.js to test.ts, by making it a .ts should allow it to be recognised in your index file.

可以找到完整的聊天记录,直到这里. 在此处

Full chat history to get to this point can be found here. Repository used to test found here

好吧,就像@ maaz-syed-adeeb提到的那样容易解决:

OK so easy solution as @maaz-syed-adeeb mentions will work:

import { path } from './lib/test.js'

扩展名之所以重要,是因为在打字稿环境中,定义文件比javascript文件具有更高的优先级.这就是为什么模块导入被取消的原因.

The reason the extension is important is because the definition file takes priority over a javascript file in a typescript environment. That is why the module import was blowing up.

为避免指定.js扩展名,您还可以像这样设置目录结构:

To avoid specifying the .js extension you can also setup your directory structure like this:

src
|- index.ts
|- lib
  |- test.js
  |- test.d.ts
  |- index.[js|ts]

文件中的

全部从测试中导出:

in ./lib/index file export all from test:

//./src/lib/index.[js|ts]
export * from './test'

,然后从lib导入所有内容:

and then import all from lib:

// ./src/index.ts
import { path } from './lib'
// or
import { path } from './lib/test.js'

如果您使用的是javascript和打字稿的混合体(例如,您要迁移到具有现有代码库的打字稿),则需要更新您的tsconfig.json以使其包含在内,这样您就不会在IDE中收到警告:

If you are using a blend of javascript and typescript (say you are moving over to typescript with an existing code base), you will need to update your tsconfig.json to include so you don't get the warnings in your IDE:

{
  "compilerOptions": {
    "allowJs": true,
    "declaration": false
  }
}

因此,您的javascript文件将与打字稿文件一起被编译到目标目录中.

This is so your javascript files will be transpiled to your destination directory along with the typescript files.

这篇关于NodeJS如何将JS文件导入TypeScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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