下面是提出这个问题时gulp-intercept的类型定义:
/// <reference types="node" />
import Vinyl = require('vinyl');
declare namespace intercept {
interface Intercept {
(interceptFunction: InterceptFunction): NodeJS.ReadWriteStream;
}
interface InterceptFunction {
(file: Vinyl): Vinyl;
}
}
declare var intercept: intercept.Intercept;
export = intercept;Vynil的Typings允许定义自定义属性。因此,如果我们在intercept函数中编写类似file.customProperty = 1的代码,就不会出现TypeScript错误。然而,对于自动完成,我想扩展Vynyl接口并重写类型,如:
import Vinyl = require('vinyl');
declare namespace intercept {
interface Intercept<VinylFile__PossiblyWithCustomProperties extends Vinyl> {
(interceptFunction: InterceptFunction<VinylFile__PossiblyWithCustomProperties>): NodeJS.ReadWriteStream;
}
interface InterceptFunction<VinylFile__PossiblyWithCustomProperties extends Vinyl> {
(file: VinylFile__PossiblyWithCustomProperties): VinylFile__PossiblyWithCustomProperties;
}
}
declare var intercept: intercept.Intercept;
export = intercept;行declare var intercept: intercept.Intercept中有错误
TS2314: Generic type `VinylFile__PossiblyWithCustomProperties` requires 1 argument(s).在这里,我们不知道将使用哪个Vynil超集,因此我不确定declare var intercept: intercept.Intercept<Vynil>;是否正确。
发布于 2019-12-21 08:57:42
我不确定我是否完全理解了这种情况,但如果您只是想编译这个示例,请在Intercept的函数级别注释类型参数VinylFile__PossiblyWithCustomProperties (在下面重命名为T ),而不是作为顶级接口声明的一部分:
import Vinyl = require("vinyl");
declare namespace intercept {
interface Intercept {
// annotate type parameter T directly at function
<T extends Vinyl>(interceptFunction: InterceptFunction<T>): NodeJS.ReadWriteStream;
}
interface InterceptFunction<T extends Vinyl> {
(file: T): T;
}
}
declare var intercept: intercept.Intercept;
export = intercept;客户端中的调用示例:
type ExtendedFile = Vinyl & { foo: string };
declare const callback: (file: ExtendedFile) => ExtendedFile;
intercept(callback);https://stackoverflow.com/questions/59366722
复制相似问题