gulp-load-plugins提供的“ gulp-postcss不是函数”类型错误

时间:2019-04-05 09:32:08

标签: javascript node.js typescript gulp postcss

gulp-load-plugins的类型定义或错误定义的gulp-postcss类型扩展TypeScirpt接口IGulpPlugins时,我会犯某种错误。

根据@types/gulp-load-pluginsindex.d.ts中的评论:

/**
 * Extend this interface to use Gulp plugins in your gulpfile.js
 */
interface IGulpPlugins {
}

gulp-postcss以外,以下所有插件均有效。

import * as gulpDebug from 'gulp-debug';
import * as gulpPlumber from 'gulp-plumber';
import * as gulpIntercept from 'gulp-intercept';
import * as gulpIf from 'gulp-if';
import * as gulpPug from 'gulp-pug';
import * aas gulpHtmlPrettify from 'gulp-html-prettify';
import * as gulpSass from 'gulp-sass';
import * as gulpStylus from 'gulp-stylus';
import * as gulpSourcemaps from 'gulp-sourcemaps';
import * as gulpPostCss from 'gulp-postcss';

export interface IGulpPlugins {
  plumber: typeof gulpPlumber;
  debug: typeof gulpDebug;
  intercept: typeof gulpIntercept;
  if: typeof gulpIf;
  pug: typeof gulpPug;
  htmlPrettify: typeof gulpHtmlPrettify;
  sass: typeof gulpSass;
  stylus: typeof gulpStylus;
  postCss: typeof gulpPostCss;
  sourcemaps: typeof gulpSourcemaps;
}

下面的代码中没有TypseScript编译错误,但是,如果执行编译的TypeScript,则会发生TypeError: gulpPlugins.postCss is not a function错误。

import * as gulp from 'gulp';
import * as GulpPlugins from 'gulp-load-plugins';

// I did not create types for this libary yet, so temporary make TypeScript to ignore it
// @ts-ignore
import * as postcssNormalizeWhitespace from 'postcss-normalize-whitespace';

gulpPlugins: IGulpPlugins = GulpPlugins();

return gulp.src(SOURCE_FILES_GLOB_SELECTION)
    .pipe(gulpPlugins.postCss([postcssNormalizeWhitespace]))
    .pipe(gulp.dest('build'));

如果要写

import * as gulp from 'gulp';
import * as GulpPlugins from 'gulp-load-plugins';

/* When I wrote this question, types in DefinitelyTyped for this library was not exists yet,
 * but I created it. However did not create the pull request yet.
 */
import * as gulpPostCss from 'gulp-postcss';

// I did not create types for this libary yet, so temporary make typescript ingore it
// @ts-ignore
import * as postcssNormalizeWhitespace from 'postcss-normalize-whitespace';


return gulp.src(SOURCE_FILES_GLOB_SELECTION)
    .pipe(gulpPostCss([postcssNormalizeWhitespace]))
    .pipe(gulp.dest('build'));

相反,JavaScript可以正常运行。

我不明白,我在哪里弄错了。 让我重复一遍,除了gulp-postcss以外,扩展的IGulpPl​​ugins中的所有插件都有效。

我写的gulp-postcss的类型是:

/// <reference types="node"/>
import Vinyl = require('vinyl');

declare function GulpPostCss(plugins?: Array<string>, options?: GulpPostCss.Options): NodeJS.ReadWriteStream;
declare function GulpPostCss(callback?: (file: Vinyl) => { plugins?: Array<string>, options?: GulpPostCss.Options }): NodeJS.ReadWriteStream;

declare namespace GulpPostCss {
    interface Options {
        parser?: string;
    }
}

export = GulpPostCss;

1 个答案:

答案 0 :(得分:2)

有两件事你做错了。

  1. 您输入了错误,或者可能不了解gulp-load-plugins的工作方式。

如果您的插件名称为gulp-post-css,则它将变为gulpPlugins.postCss。但是,该插件名称为gulp-postcss,因此它将为:

gulpPlugins.postcss(...) // NOT gulpPlugins.postCss !!!
  1. 您不懂打字稿。

这与打字稿无关。

这是一个经验法则:打字稿对javascript运行时行为的影响为。每当您发现运行时错误时,它都必须来自打字稿的JavaScript端。

尽管1.已经解决了您的问题,但让我们假装您必须使用gulpPlugins.postCss。为了使它起作用,请尝试添加以下行:

import * as gulpPostCss from 'gulp-postcss';
gulpPlugins.postCss = gulpPostCss;

现在可以了。