将JS文件导入Typescript [英] Importing JS File into Typescript

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

问题描述

我正在寻找转移到Typescript,并且目前正在考虑逐个文件地缓慢执行此操作。

I'm looking at moving over to Typescript and currently looking at doing this slowly if possible file by file.

现在我的系统是用Webpack构建的我想继续这个来构建我的整体包。

Now the system I currently have is built with Webpack and I'm wanting to continue this for building my overall bundle.

我有一个.d.ts文件的定义,但我需要我的文件继续导入当前抛出错误。

I have a .d.ts file for the definition but I need my file to continue importing which currently is throwing an error.

/// <reference path="../../../../vendor/src/foo.d.ts" />
import foo = require('../../../../vendor/src/foo.js');

foo('something');

这是当前抛出的错误我也试过没有参考路径这似乎引发了一个错误它不是一个模块。目前我唯一没有引发错误的方法是导入并只是添加引用,但这意味着webpack不会知道我的文件的位置。

This currently is throwing errors I have also tried without the reference path and this seems to raise an error that it is not a module. Currently the only way I can have no errors raised is not importing and simply adding the reference but this will mean that webpack will not know the location of my file.

任何想法我已经搜索了很多,但我真的无法理解将JS文件导入Typescript。

Any ideas I have searched quite a lot but I can't really make sense of importing JS files into Typescript.

推荐答案


我搜索过的任何想法都很多,但我无法理解将JS文件导入Typescript。

Any ideas I have searched quite a lot but I can't really make sense of importing JS files into Typescript.

For webpack所需要的只是声明 require 函数并执行您已经习惯的操作:

For webpack all you need is to declare the require function and do what you are already used to:

var foo = require('../../../../vendor/src/foo');

这里有 require 函数类型定义: https://github.com/TypeStrong/ts- loader #loading-other-resources-and-code-splitting

The require function type definition is present here : https://github.com/TypeStrong/ts-loader#loading-other-resources-and-code-splitting

如果你想使用 foo 以更强的方式...建议将其移植到 .ts ,而不是努力构建然后维护一个 .d.ts 的文件。

If you want to use foo in a stronger manner... recommend porting it over to .ts as well instead of struggling to build and then maintain a .d.ts file for it.

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

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