简体   繁体   English

导入FastClick

[英]Import FastClick

I'm using FastClick with FastClick.d.ts . 我正在使用FastClickFastClick.d.ts TSC is using module: "commonjs" and I'm bundling with Webpack. TSC正在使用module: "commonjs" ,我正在捆绑Webpack。 I can't figure out how to reference FastClick. 我无法弄清楚如何引用FastClick。

How can I import FastClick into TypeScript? 如何将FastClick导入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: 我没有得到TSC编译错误,但是转换后的代码如下所示:

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

Which doesn't work. 哪个不起作用。 fastclick_1 appears to be the FastClick function itself. fastclick_1似乎是FastClick函数本身。

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: 我得到一个编译错误Error:(6, 49) TS2339: Property 'attach' does not exist on type 'typeof fastclick' ,但是发出的JS工作:

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

So how can I get TSC and the emitted JS to both work? 那么我怎样才能让TSC和发出的JS都工作呢? Is the FastClick.d.ts wrong? FastClick.d.ts错了吗? Am I importing the module wrong? 我导入模块错了吗?

@basarat never merged his pull request. @basarat从未合并他的拉请求。 Calling attach via bracket notation will prevent the TSC error and emit the proper JS. 通过括号表示法调用attach将阻止TSC错误并发出正确的JS。

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

It's not ideal, but it works. 它并不理想,但它有效。

Is the FastClick.d.ts wrong FastClick.d.ts是错误的

Yes. 是。 Definitely Typed is best effort (like most documentation efforts disconnected from source) and wrong in this case. 绝对Typed是最好的努力(就像大多数文档工作从源代码断开),在这种情况下是错误的。

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

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