从gulp-load-plugins的类型定义或错误定义的gulp-postcss类型扩展TypeScirpt接口IGulpPlugins
时,我会犯某种错误。
根据@types/gulp-load-plugins的index.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
以外,扩展的IGulpPlugins中的所有插件都有效。
我写的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;
答案 0 :(得分:2)
有两件事你做错了。
gulp-load-plugins
的工作方式。如果您的插件名称为gulp-post-css
,则它将变为gulpPlugins.postCss
。但是,该插件名称为gulp-postcss
,因此它将为:
gulpPlugins.postcss(...) // NOT gulpPlugins.postCss !!!
这与打字稿无关。
这是一个经验法则:打字稿对javascript运行时行为的影响为零。每当您发现运行时错误时,它都必须来自打字稿的JavaScript端。
尽管1.已经解决了您的问题,但让我们假装您必须使用gulpPlugins.postCss
。为了使它起作用,请尝试添加以下行:
import * as gulpPostCss from 'gulp-postcss';
gulpPlugins.postCss = gulpPostCss;
现在可以了。