简体   繁体   中英

Import FastClick

I'm using FastClick with FastClick.d.ts . TSC is using module: "commonjs" and I'm bundling with Webpack. I can't figure out how to reference FastClick.

How can I import FastClick into TypeScript? If I do this:

import {FastClick} from 'fastclick'
FastClick.attach(document.body);

I get no TSC compile errors, but the transpiled code looks like this:

var fastclick_1 = require('fastclick');
fastclick_1.FastClick.attach(document.body)

Which doesn't work. fastclick_1 appears to be the FastClick function itself.

If I do this:

import * as FastClick from 'fastclick'
FastClick.attach(document.body)

I get a compile error Error:(6, 49) TS2339: Property 'attach' does not exist on type 'typeof fastclick' , but the emitted JS works:

var FastClick = require('fastclick');
FastClick.attach(document.body);

So how can I get TSC and the emitted JS to both work? Is the FastClick.d.ts wrong? Am I importing the module wrong?

@basarat never merged his pull request. Calling attach via bracket notation will prevent the TSC error and emit the proper JS.

import * as FastClick from 'fastclick';
FastClick['attach'](document.body);

It's not ideal, but it works.

Is the FastClick.d.ts wrong

Yes. Definitely Typed is best effort (like most documentation efforts disconnected from source) and wrong in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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