简体   繁体   English

如何在Typescript 1.8中包含无类型的节点模块?

[英]How to include untyped node modules with Typescript 1.8?

Typescript 1.8 now supports untyped JS files. Typescript 1.8现在支持无类型的JS文件。 To enable this feature, just add the compiler flag --allowJs or add "allowJs": true to compilerOptions in tsconfig.json 要启用此功能,只需添加编译器标志--allowJs或将“allowJs”:true添加到tsconfig.json中的compilerOptions

via https://blogs.msdn.microsoft.com/typescript/2016/01/28/announcing-typescript-1-8-beta/ 通过https://blogs.msdn.microsoft.com/typescript/2016/01/28/announcing-typescript-1-8-beta/

I'm trying to import react-tap-event-plugin which does not have a typings file. 我正在尝试导入没有打字文件的react-tap-event-plugin

import * as injectTapEventPlugin from 'injectTapEventPlugin'; 

says module not found. 说找不到模块。 So i tried: 所以我试过:

import * as injectTapEventPlugin from '../node_modules/react-tap-event-plugin/src/injectTapEventPlugin.js';

This says Module resolves to a non-module entity and cannot be imported using this construct. 这表示模块解析为非模块实体,无法使用此结构导入。 And then I tried: 然后我尝试了:

import injectTapEventPlugin = require('../node_modules/react-tap-event-plugin/src/injectTapEventPlugin.js');

It's crashing with ERROR in ./scripts/index.tsx Module build failed: TypeError: Cannot read property 'kind' of undefined at node_modules/typescript/lib/typescript.js:39567 ERROR in ./scripts/index.tsx Module build failed: TypeError: Cannot read property 'kind' of undefinedERROR in ./scripts/index.tsx Module build failed: TypeError: Cannot read property 'kind' of undefined崩溃ERROR in ./scripts/index.tsx Module build failed: TypeError: Cannot read property 'kind' of undefined node_modules/typescript/lib/typescript.js:39567 ERROR in ./scripts/index.tsx Module build failed: TypeError: Cannot read property 'kind' of undefined node_modules/typescript/lib/typescript.js:39567

My tsconfig: 我的tsconfig:

{
  "compilerOptions": {
  "target": "ES5",
  "removeComments": true,
  "jsx": "react",
  "module": "commonjs",
  "sourceMap": true,
  "allowJs": true
  },
  "exclude": [
    "node_modules"
  ]
}

I'm using webpack with ts-loader: 我正在使用带有ts-loader的webpack:

 {
   test: /\.tsx?$/,
   exclude: ['node_modules', 'tests'],
   loader: 'ts-loader'
 }

The new --allowJs feature doesn't mean that typings will be generated for you, so you can't do 新的--allowJs功能并不意味着将为您生成打字,因此您无法做到

import {SomeModule} from 'something';

where 'something' is a plain JS file - you have to import it using plain JS syntax, eg 其中'something'是一个普通的JS文件 - 你必须使用普通的JS语法导入它,例如

var someModule = require('something');

If you don't have a .d.ts file for the import, you won't be able to use any type annotations, eg 如果您没有用于导入的.d.ts文件,则无法使用任何类型的注释,例如

let x: someModule

will be invalid. 将无效。 If you want type annotations, intellisense, and any other TypeScript features for the import, you'll have to come up with a .d.ts file for it, or create your own interfaces for the bits you need. 如果您想要导入类型注释,智能感知和任何其他TypeScript功能,则必须为其提供.d.ts文件,或者为您需要的位创建自己的接口。

Reading the documentation, it appears this feature is mainly for people converting from .js to .ts so that they can incrementally rewrite their .js files. 阅读文档,看来这个功能主要是为了从.js转换为.ts的人,以便他们可以逐步重写他们的.js文件。 As well, it's used to bundle your externals with your own code, and saves you having to bundle/concatenate files using a tool like webpack or browserify. 同样,它用于将您的外部代码与您自己的代码捆绑在一起,并且使您可以使用webpack或browserify等工具捆绑/连接文件。

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

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