最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to include untyped node modules with Typescript 1.8? - Stack Overflow

programmeradmin2浏览0评论

Typescript 1.8 now supports untyped JS files. To enable this feature, just add the piler flag --allowJs or add "allowJs": true to pilerOptions in tsconfig.json

via /

I'm trying to import react-tap-event-plugin which does not have a typings file.

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

My tsconfig:

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

I'm using webpack with ts-loader:

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

Typescript 1.8 now supports untyped JS files. To enable this feature, just add the piler flag --allowJs or add "allowJs": true to pilerOptions in tsconfig.json

via https://blogs.msdn.microsoft./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.

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

My tsconfig:

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

I'm using webpack with ts-loader:

 {
   test: /\.tsx?$/,
   exclude: ['node_modules', 'tests'],
   loader: 'ts-loader'
 }
Share Improve this question asked Feb 4, 2016 at 12:43 Frozen CrayonFrozen Crayon 5,4208 gold badges38 silver badges72 bronze badges 1
  • I added pull request for react-tap-event-plugin and it's already merged. github./DefinitelyTyped/DefinitelyTyped/pull/8260 – mixel Commented Feb 26, 2016 at 14:47
Add a ment  | 

1 Answer 1

Reset to default 5

The new --allowJs feature doesn't mean that typings will be generated for you, so you can't do

import {SomeModule} from 'something';

where 'something' is a plain JS file - you have to import it using plain JS syntax, e.g.

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, e.g.

let x: someModule

will be invalid. If you want type annotations, intellisense, and any other TypeScript features for the import, you'll have to e up with a .d.ts file for it, or create your own interfaces for the bits you need.

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. 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.

发布评论

评论列表(0)

  1. 暂无评论